Skip to content

Commit 9dfdec6

Browse files
committed
Allow for usage of snowplow callback without this
1 parent 5f01e36 commit 9dfdec6

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

trackers/javascript-tracker/src/in_queue.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ export function InQueueManager(functionName: string, asyncQueue: Array<unknown>)
265265
// Strip GlobalSnowplowNamespace from ID
266266
fnTrackers[tracker.id.replace(`${functionName}_`, '')] = tracker;
267267
}
268+
269+
// Append the `fnTrackers` object to the parameterArray, to allow it to be
270+
// accessed as the final argument in the callback, useful in environments that don't support `this` (GTM)
271+
Array.prototype.push.call(parameterArray, fnTrackers);
272+
268273
input.apply(fnTrackers, parameterArray);
269274
} catch (ex) {
270275
LOG.error('Tracker callback failed', ex);

trackers/javascript-tracker/test/unit/in_queue.test.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ describe('InQueueManager', () => {
6767
setUserId: function (s?: string | null) {
6868
userId = s;
6969
},
70+
getUserId: function () {
71+
return userId;
72+
},
7073
trackPageView: function () {
7174
output = attribute;
7275
},
@@ -183,4 +186,100 @@ describe('InQueueManager', () => {
183186
]);
184187
}).not.toThrow();
185188
});
189+
190+
it('Executing a custom callback with arguments should pass the arguments to the callback parameters', () => {
191+
asyncQueue.push([
192+
function (a: number, b: number) {
193+
expect(a).toEqual(1);
194+
expect(b).toEqual(2);
195+
},
196+
1,
197+
2,
198+
]);
199+
});
200+
201+
it('A custom callback with arguments provided will pass those arguments into the callback parameters', () => {
202+
const tracker = newTracker('sp1');
203+
mockTrackers.sp1 = tracker;
204+
205+
asyncQueue.push([
206+
function (a: number, b: number) {
207+
expect(a).toEqual(1);
208+
expect(b).toEqual(2);
209+
},
210+
1,
211+
2,
212+
]);
213+
214+
delete mockTrackers.sp1;
215+
});
216+
217+
it('The callback will be passed the tracker dictionary as the final argument', () => {
218+
const mockTracker = newTracker('sp1');
219+
mockTracker.setUserId('foo');
220+
mockTrackers.sp1 = mockTracker;
221+
222+
asyncQueue.push([
223+
function (a: number, b: number, trackers: Record<string, any>) {
224+
expect(a).toEqual(1);
225+
expect(b).toEqual(2);
226+
227+
const tracker = trackers.sp1;
228+
229+
expect(tracker).toEqual(mockTracker);
230+
expect(tracker.getUserId()).toEqual('foo');
231+
},
232+
1,
233+
2,
234+
]);
235+
delete mockTrackers.sp1;
236+
});
237+
238+
it('The callback will be passed the tracker dictionary as the argument if there is only one parameter', () => {
239+
const mockTracker = newTracker('sp1');
240+
mockTracker.setUserId('foo');
241+
mockTrackers.sp1 = mockTracker;
242+
243+
asyncQueue.push([
244+
function (trackers: Record<string, any>) {
245+
const tracker = trackers.sp1;
246+
247+
expect(tracker).toEqual(mockTracker);
248+
expect(tracker.getUserId()).toEqual('foo');
249+
},
250+
]);
251+
delete mockTrackers.sp1;
252+
});
253+
254+
it('The callback can access the tracker dictionary using both `this` and the first argument', () => {
255+
const mockTracker = newTracker('sp1');
256+
mockTracker.setUserId('foo');
257+
mockTrackers.sp1 = mockTracker;
258+
259+
asyncQueue.push([
260+
function (this: any, trackers: Record<string, any>) {
261+
expect(this.sp1).toEqual(mockTrackers.sp1);
262+
expect(trackers.sp1).toEqual(mockTrackers.sp1);
263+
},
264+
]);
265+
delete mockTrackers.sp1;
266+
});
267+
268+
it('The callback can access the tracker dictionary using both `this` and the last argument, along with arguments', () => {
269+
const mockTracker = newTracker('sp1');
270+
mockTracker.setUserId('foo');
271+
mockTrackers.sp1 = mockTracker;
272+
273+
asyncQueue.push([
274+
function (this: any, a: number, b: number, trackers: Record<string, any>) {
275+
expect(this.sp1).toEqual(mockTrackers.sp1);
276+
expect(a).toEqual(1);
277+
expect(b).toEqual(2);
278+
expect(trackers.sp1).toEqual(mockTrackers.sp1);
279+
},
280+
1,
281+
2,
282+
]);
283+
delete mockTrackers.sp1;
284+
});
186285
});

0 commit comments

Comments
 (0)