@@ -16,6 +16,11 @@ define(
16
16
hasAddedParameters : false ,
17
17
useCookie : false ,
18
18
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
+
19
24
track : function ( algoliaConfig , partial = false ) {
20
25
this . config = algoliaConfig ;
21
26
this . defaultIndexName = algoliaConfig . indexName + '_products' ;
@@ -68,34 +73,34 @@ define(
68
73
}
69
74
} ,
70
75
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
+
71
89
addSearchParameters : function ( ) {
72
90
if ( this . hasAddedParameters ) {
73
91
return ;
74
92
}
75
93
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
+ ) ;
86
98
87
99
return allWidgetConfiguration ;
88
100
} ) ;
89
101
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 ) ;
99
104
} ) ;
100
105
101
106
this . hasAddedParameters = true ;
0 commit comments