Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/analytics-browser/src/gtm-snippet-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { runQueuedFunctions } from './utils/snippet-helper';
GlobalScope.amplitudeGTM._q = [];
runQueuedFunctions(amplitudeGTM, queue);

const instanceNames = Object.keys(GlobalScope.amplitudeGTM._iq) || [];
const instanceNames = Object.keys(GlobalScope.amplitudeGTM._iq || {});
Comment thread
Mercy811 marked this conversation as resolved.
Outdated
for (let i = 0; i < instanceNames.length; i++) {
const instanceName = instanceNames[i];
const instance = Object.assign(GlobalScope.amplitudeGTM._iq[instanceName], createNamedInstance(instanceName));
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-browser/src/snippet-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function resolveCurrentScriptUrl(): string | undefined {
GlobalScope.amplitude._q = [];
runQueuedFunctions(amplitude, queue);

const instanceNames = Object.keys(GlobalScope.amplitude._iq) || [];
const instanceNames = Object.keys(GlobalScope.amplitude._iq || {});
for (let i = 0; i < instanceNames.length; i++) {
const instanceName = instanceNames[i];
const instance = Object.assign(GlobalScope.amplitude._iq[instanceName], createNamedInstance(instanceName));
Expand Down
3 changes: 3 additions & 0 deletions packages/analytics-browser/src/utils/snippet-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const runQueuedFunctions = (instance: object, queue: QueueProxy) => {
* Used to convert proxied Identify and Revenue objects.
*/
export const convertProxyObjectToRealObject = <T>(instance: T, queue: QueueProxy): T => {
if (!queue) {
return instance;
}
Comment thread
Mercy811 marked this conversation as resolved.
Outdated
for (let i = 0; i < queue.length; i++) {
const { name, args, resolve } = queue[i];
const fn = instance && instance[name as keyof T];
Expand Down
11 changes: 11 additions & 0 deletions packages/analytics-browser/test/utils/snippet-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,16 @@ describe('snippet-helper', () => {
expect(SnippetHelper.convertProxyObjectToRealObject(instance, queue)).toBe(instance);
expect(resolve).toHaveBeenCalledTimes(1);
});

test('should return instance when queue is undefined', () => {
const instance = { init: jest.fn() };
expect(
SnippetHelper.convertProxyObjectToRealObject(
instance,
undefined as unknown as Parameters<typeof SnippetHelper.convertProxyObjectToRealObject>[1],
),
).toBe(instance);
Comment thread
Mercy811 marked this conversation as resolved.
Outdated
expect(instance.init).not.toHaveBeenCalled();
});
});
});
Loading