Skip to content

Commit 5ad5942

Browse files
authored
Merge pull request #1512 from algolia/feature/MAGE-881
MAGE-881: Ensure token is sent for either click and conversions or perso
2 parents 5f4f2c1 + a3d3b33 commit 5ad5942

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

Helper/InsightsHelper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class InsightsHelper
1717

1818
public const ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME = 'aa-search';
1919

20+
/**
21+
* Up to 129 chars per https://www.algolia.com/doc/rest-api/insights/#method-param-usertoken
22+
* But capping at legacy 64 chars for backward compat
23+
*/
24+
/** @var int */
25+
public const ALGOLIA_USER_TOKEN_MAX_LENGTH = 64;
26+
2027
/** @var ConfigHelper */
2128
private $configHelper;
2229

@@ -25,7 +32,7 @@ class InsightsHelper
2532

2633
/** @var SessionManagerInterface */
2734
private $sessionManager;
28-
35+
2936
/** @var CookieManagerInterface */
3037
private $cookieManager;
3138

view/frontend/web/insights.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ define(
1616
hasAddedParameters: false,
1717
useCookie: false,
1818

19+
// Although events can accept both auth and anon tokens, queries can only accept a single token
20+
determineUserToken() {
21+
return algoliaAnalytics.getAuthenticatedUserToken() ?? algoliaAnalytics.getUserToken();
22+
},
23+
1924
track: function (algoliaConfig, partial = false) {
2025
this.config = algoliaConfig;
2126
this.defaultIndexName = algoliaConfig.indexName + '_products';
@@ -68,34 +73,34 @@ define(
6873
}
6974
},
7075

76+
applyInsightsToSearchParams(params = {}) {
77+
if (algoliaConfig.ccAnalytics.enabled) {
78+
params.clickAnalytics = true;
79+
}
80+
81+
if (algoliaConfig.personalization.enabled) {
82+
params.enablePersonalization = true;
83+
params.userToken = this.determineUserToken();
84+
}
85+
86+
return params;
87+
},
88+
7189
addSearchParameters: function () {
7290
if (this.hasAddedParameters) {
7391
return;
7492
}
7593

76-
algolia.registerHook('beforeWidgetInitialization', function (allWidgetConfiguration) {
77-
allWidgetConfiguration.configure = allWidgetConfiguration.configure || {};
78-
if (algoliaConfig.ccAnalytics.enabled) {
79-
allWidgetConfiguration.configure.clickAnalytics = true;
80-
}
81-
82-
if (algoliaConfig.personalization.enabled) {
83-
allWidgetConfiguration.configure.enablePersonalization = true;
84-
allWidgetConfiguration.configure.userToken = algoliaAnalytics.getUserToken();
85-
}
94+
algolia.registerHook('beforeWidgetInitialization', (allWidgetConfiguration) => {
95+
allWidgetConfiguration.configure = algoliaInsights.applyInsightsToSearchParams(
96+
allWidgetConfiguration.configure
97+
);
8698

8799
return allWidgetConfiguration;
88100
});
89101

90-
algolia.registerHook('afterAutocompleteProductSourceOptions', function (options) {
91-
if (algoliaConfig.ccAnalytics.enabled) {
92-
options.clickAnalytics = true;
93-
}
94-
if (algoliaConfig.personalization.enabled) {
95-
options.enablePersonalization = true;
96-
options.userToken = algoliaAnalytics.getUserToken();
97-
}
98-
return options;
102+
algolia.registerHook('afterAutocompleteProductSourceOptions', (options) => {
103+
return algoliaInsights.applyInsightsToSearchParams(options);
99104
});
100105

101106
this.hasAddedParameters = true;

0 commit comments

Comments
 (0)