Skip to content

Commit

Permalink
dataLayer reference
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 19, 2024
1 parent 8f4e090 commit 777daa7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 36 deletions.
15 changes: 3 additions & 12 deletions packages/sources/datalayer/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('source dataLayer', () => {
});

const gtag: Gtag.Gtag = function () {
dataLayer.push(arguments);
window.dataLayer!.push(arguments);
};

test('init new', () => {
Expand All @@ -37,15 +37,6 @@ describe('source dataLayer', () => {
expect(originalPush).not.toBe(dataLayer!.push);
});

test('config dataLayer', () => {
const dataLayer: DataLayer = [];

sourceDataLayer({ elb, dataLayer, name: 'foo' });
expect(window.foo).toBeUndefined(); // Prefer dataLayer over name
dataLayer.push({ event: 'foo' });
expect(elb).toHaveBeenCalledTimes(1);
});

test('config name', () => {
expect(window.foo).toBeUndefined();

Expand Down Expand Up @@ -90,7 +81,7 @@ describe('source dataLayer', () => {
});

test('arguments', () => {
dataLayer = [
window.dataLayer = [
{
event: 'gtm.js',
'gtm.start': 1730909886667,
Expand All @@ -103,7 +94,7 @@ describe('source dataLayer', () => {
}),
];

sourceDataLayer({ elb, dataLayer });
sourceDataLayer({ elb });

gtag('event', 'another_arg', {
bar: 'baz',
Expand Down
17 changes: 10 additions & 7 deletions packages/sources/datalayer/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { DataLayer } from '../types';
import { sourceDataLayer } from '..';

describe('mapping', () => {
const elb = jest.fn(); //.mockImplementation(console.log);
let dataLayer: DataLayer;

function gtag(...args: unknown[]) {
args;
Expand All @@ -28,7 +30,8 @@ describe('mapping', () => {

beforeEach(() => {
jest.clearAllMocks();
window.dataLayer = undefined;
window.dataLayer = [];
dataLayer = window.dataLayer as DataLayer;
});

test('init dataLayer', () => {
Expand Down Expand Up @@ -70,7 +73,7 @@ describe('mapping', () => {
});

test('default values', () => {
const { dataLayer } = sourceDataLayer({ elb })!;
sourceDataLayer({ elb })!;

dataLayer.push({ event: 'foo this' });
expect(elb).toHaveBeenCalledWith({
Expand All @@ -84,7 +87,7 @@ describe('mapping', () => {
});

test('mapping name', () => {
const { dataLayer } = sourceDataLayer({
sourceDataLayer({
elb,
mapping: {
foo: { name: 'bar' },
Expand All @@ -104,7 +107,7 @@ describe('mapping', () => {
});

test('mapping ignore', () => {
const { dataLayer } = sourceDataLayer({
sourceDataLayer({
elb,
mapping: {
foo: {
Expand All @@ -118,7 +121,7 @@ describe('mapping', () => {
});

test('mapping *', () => {
const { dataLayer } = sourceDataLayer({
sourceDataLayer({
elb,
mapping: {
'*': { ignore: true },
Expand Down Expand Up @@ -221,7 +224,7 @@ describe('mapping', () => {
});

test('mapping add_to_cart', () => {
const { dataLayer } = sourceDataLayer({
sourceDataLayer({
elb,
mapping: {
add_to_cart: {
Expand Down Expand Up @@ -276,7 +279,7 @@ describe('mapping', () => {
});

test('mapping purchase', () => {
const { dataLayer } = sourceDataLayer({
sourceDataLayer({
elb,
mapping: {
purchase: {
Expand Down
9 changes: 9 additions & 0 deletions packages/sources/datalayer/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DataLayer } from './types';

export function convertConsentStates(
obj: Record<string, unknown>,
): Record<string, unknown> {
Expand All @@ -10,3 +12,10 @@ export function convertConsentStates(

return consent;
}

export function getDataLayer(name = 'dataLayer'): DataLayer {
// Ensure the dataLayer exists
if (!window[name]) window[name] = [];

return window[name] || [];
}
19 changes: 4 additions & 15 deletions packages/sources/datalayer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import type { Config, DataLayer } from './types';
import type { Config } from './types';
import { intercept, push } from './push';
import { getDataLayer } from './helper';

export * as SourceDataLayer from './types';

export function sourceDataLayer(
partialConfig: Partial<Config> = {},
): Config | undefined {
const { elb, prefix = 'dataLayer', skipped = [] } = partialConfig;
const { elb, name, prefix = 'dataLayer', skipped = [] } = partialConfig;
if (!elb) return;

let { dataLayer } = partialConfig;

// Ensure the dataLayer exists
if (!dataLayer) {
const { name = 'dataLayer' } = partialConfig;
const key = name as keyof Window;

// Ensure the dataLayer exists
if (!window[key]) (window[key] as unknown) = [];

dataLayer = window[key] as DataLayer;
}
const dataLayer = getDataLayer(name);

const config: Config = {
...partialConfig,
elb,
dataLayer,
prefix,
skipped,
};
Expand Down
3 changes: 2 additions & 1 deletion packages/sources/datalayer/src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {
tryCatch,
} from '@elbwalker/utils';
import { objToEvent, gtagToObj } from './mapping';
import { getDataLayer } from './helper';

export function intercept(config: Config) {
const { dataLayer } = config;
const dataLayer = getDataLayer(config.name);

// Store the original push function to preserve existing functionality
const dataLayerPush = dataLayer.push.bind(dataLayer);
Expand Down
1 change: 0 additions & 1 deletion packages/sources/datalayer/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ declare global {
export type DataLayer = Array<unknown>;
export interface Config {
elb: WalkerOS.Elb | WalkerOS.AnyFunction;
dataLayer: DataLayer;
mapping?: Mapping;
name?: string;
prefix: string;
Expand Down

0 comments on commit 777daa7

Please sign in to comment.