Skip to content

Commit 8241b29

Browse files
Eunjae Leefrancoischalifour
Eunjae Lee
andauthoredApr 28, 2021
fix(insights): do not throw when userToken is not given (#4724)
Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
1 parent 1a939c0 commit 8241b29

File tree

3 files changed

+106
-18
lines changed

3 files changed

+106
-18
lines changed
 

‎src/middlewares/__tests__/createInsightsMiddleware.ts

+91-15
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ describe('insights', () => {
103103
});
104104
});
105105

106-
it('does not throw when an event is sent right after the creation in UMD', () => {
106+
it('does not throw without userToken in UMD with the library loaded after the event', () => {
107107
const {
108-
analytics,
109108
insightsClient,
110109
libraryLoadedAndProcessQueue,
111110
instantSearchInstance,
112111
} = createUmdTestEnvironment();
113112

114113
const middleware = createInsightsMiddleware({
115114
insightsClient,
115+
insightsInitParams: { useCookie: false },
116116
})({ instantSearchInstance });
117117
middleware.subscribe();
118118

@@ -127,22 +127,98 @@ describe('insights', () => {
127127
},
128128
widgetType: 'ais.hits',
129129
});
130-
expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(0);
131-
132-
// But, the library hasn't been loaded yet, so the event stays in the queue.
133-
expect(insightsClient.queue[insightsClient.queue.length - 1]).toEqual([
134-
'viewedObjectIDs',
135-
{ eventName: 'Hits Viewed', index: '', objectIDs: ['1', '2'] },
136-
]);
137130

138131
// When the library is loaded later, it consumes the queue and sends the event.
132+
expect(() => {
133+
libraryLoadedAndProcessQueue();
134+
}).not.toThrow(
135+
"Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload."
136+
);
137+
});
138+
139+
it('does not throw without userToken in UMD with the library loaded before the event', () => {
140+
const {
141+
insightsClient,
142+
libraryLoadedAndProcessQueue,
143+
instantSearchInstance,
144+
} = createUmdTestEnvironment();
145+
146+
const middleware = createInsightsMiddleware({
147+
insightsClient,
148+
insightsInitParams: { useCookie: false },
149+
})({ instantSearchInstance });
150+
middleware.subscribe();
151+
139152
libraryLoadedAndProcessQueue();
140-
expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(1);
141-
expect(analytics.viewedObjectIDs).toHaveBeenCalledWith({
142-
eventName: 'Hits Viewed',
143-
index: '',
144-
objectIDs: ['1', '2'],
145-
});
153+
154+
expect(() => {
155+
// It tries to send an event.
156+
instantSearchInstance.sendEventToInsights({
157+
eventType: 'view',
158+
insightsMethod: 'viewedObjectIDs',
159+
payload: {
160+
eventName: 'Hits Viewed',
161+
index: '',
162+
objectIDs: ['1', '2'],
163+
},
164+
widgetType: 'ais.hits',
165+
});
166+
}).not.toThrow(
167+
"Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload."
168+
);
169+
});
170+
171+
it('does not throw without userToken in CJS', () => {
172+
const { insightsClient, instantSearchInstance } = createTestEnvironment();
173+
174+
const middleware = createInsightsMiddleware({
175+
insightsClient,
176+
insightsInitParams: { useCookie: false },
177+
})({ instantSearchInstance });
178+
middleware.subscribe();
179+
180+
expect(() => {
181+
// It tries to send an event.
182+
instantSearchInstance.sendEventToInsights({
183+
eventType: 'view',
184+
insightsMethod: 'viewedObjectIDs',
185+
payload: {
186+
eventName: 'Hits Viewed',
187+
index: '',
188+
objectIDs: ['1', '2'],
189+
},
190+
widgetType: 'ais.hits',
191+
});
192+
}).not.toThrow(
193+
"Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload."
194+
);
195+
});
196+
197+
it('warns when userToken is not set', () => {
198+
const { insightsClient, instantSearchInstance } = createTestEnvironment();
199+
200+
const middleware = createInsightsMiddleware({
201+
insightsClient,
202+
insightsInitParams: { useCookie: false },
203+
})({ instantSearchInstance });
204+
middleware.subscribe();
205+
206+
expect(() =>
207+
instantSearchInstance.sendEventToInsights({
208+
eventType: 'view',
209+
insightsMethod: 'viewedObjectIDs',
210+
payload: {
211+
eventName: 'Hits Viewed',
212+
index: '',
213+
objectIDs: ['1', '2'],
214+
},
215+
widgetType: 'ais.hits',
216+
})
217+
).toWarnDev(
218+
`[InstantSearch.js]: Cannot send event to Algolia Insights because \`userToken\` is not set.
219+
220+
See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-further/send-insights-events/js/#setting-the-usertoken`
221+
);
146222
});
147223

148224
it('applies clickAnalytics', () => {

‎src/middlewares/createInsightsMiddleware.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
101101
);
102102
}
103103
};
104+
const hasUserToken = () => Boolean((helper.state as any).userToken);
104105

105106
helper.setState(helper.state.setQueryParameter('clickAnalytics', true));
106107

@@ -128,7 +129,18 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
128129
if (onEvent) {
129130
onEvent(event, _insightsClient);
130131
} else if (event.insightsMethod) {
131-
insightsClient(event.insightsMethod, event.payload);
132+
if (hasUserToken()) {
133+
insightsClient(event.insightsMethod, event.payload);
134+
} else {
135+
warning(
136+
false,
137+
`
138+
Cannot send event to Algolia Insights because \`userToken\` is not set.
139+
140+
See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-further/send-insights-events/js/#setting-the-usertoken
141+
`
142+
);
143+
}
132144
} else {
133145
warning(
134146
false,

‎src/types/insights.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export type InsightsAddAlgoliaAgent = (
5050
export type InsightsClient = InsightsAddAlgoliaAgent &
5151
InsightsSendEvent &
5252
InsightsOnUserTokenChange &
53-
InsightsGetUserToken &
5453
InsightsInit &
55-
InsightsSetUserToken & {
54+
InsightsSetUserToken &
55+
InsightsGetUserToken & {
5656
queue?: Array<[string, any]>;
5757
};
5858

0 commit comments

Comments
 (0)
Please sign in to comment.