Skip to content

Commit

Permalink
use filterValues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 17, 2024
1 parent c5463eb commit 80f860e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/sources/datalayer/src/__tests__/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable prefer-rest-params */
import type { DataLayer } from '../types';
import { sourceDataLayer } from '..';

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

const gtag: Gtag.Gtag = function () {
// eslint-disable-next-line prefer-rest-params
dataLayer.push(arguments);
};

Expand Down
22 changes: 20 additions & 2 deletions packages/sources/datalayer/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ describe('mapping', () => {
);
});

test('gtagToObj config', () => {
test('gtag config', () => {
sourceDataLayer({ elb });

gtag('config', 'GA-XXXXXXXXXX');
Expand Down Expand Up @@ -486,7 +486,7 @@ describe('mapping', () => {
});
});

test('gtagToObj get', () => {
test('gtag get', () => {
sourceDataLayer({ elb });

gtag('get', 'campaign');
Expand Down Expand Up @@ -525,4 +525,22 @@ describe('mapping', () => {
}),
);
});

test('filter parameters', () => {
sourceDataLayer({ elb });

gtag('event', 'foo', {
elem: document.createElement('div'),
fn: jest.fn(),
a: '',
});
expect(elb).toHaveBeenCalledWith(
expect.objectContaining({
data: {
event: 'foo',
a: '',
},
}),
);
});
});
14 changes: 0 additions & 14 deletions packages/sources/datalayer/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import type { WalkerOS } from '@elbwalker/types';
import { isObject } from '@elbwalker/utils';

export function isString(value: unknown): value is string {
return typeof value === 'string';
}

export function wasArguments(obj: unknown): obj is WalkerOS.AnyObject {
return (
isObject(obj) &&
Object.keys(obj).every((key, index) => key === String(index))
);
}

export function convertConsentStates(
obj: Record<string, unknown>,
): Record<string, unknown> {
Expand Down
10 changes: 4 additions & 6 deletions packages/sources/datalayer/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WalkerOS } from '@elbwalker/types';
import type { Config, EventConfig, MappedEvent, Mapping } from './types';
import { getId, getMappingValue, isObject } from '@elbwalker/utils';
import { convertConsentStates, isString } from './helper';
import { getId, getMappingValue, isObject, isString } from '@elbwalker/utils';
import { convertConsentStates } from './helper';

const defaultMapping: Mapping = {
'consent default': { ignore: true },
Expand Down Expand Up @@ -85,10 +85,8 @@ export function objToEvent(obj: unknown, config: Config): MappedEvent | void {
}

// https://developers.google.com/tag-platform/gtagjs/reference
export function gtagToObj(args: WalkerOS.AnyObject): WalkerOS.AnyObject | void {
const command = args[0];
const value = args[1];
const params = args[2];
export function gtagToObj(args: IArguments): WalkerOS.AnyObject | void {
const [command, value, params] = args;

let event: string | undefined;
let obj = isObject(params) ? params : {};
Expand Down
7 changes: 3 additions & 4 deletions packages/sources/datalayer/src/push.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Config } from './types';
import { clone, tryCatch } from '@elbwalker/utils';
import { clone, filterValues, isArguments, tryCatch } from '@elbwalker/utils';
import { objToEvent, gtagToObj } from './mapping';
import { wasArguments } from './helper';

export function intercept(config: Config) {
const { dataLayer } = config;
Expand All @@ -22,13 +21,13 @@ export function push(config: Config, ...args: unknown[]) {
const clonedArgs = clone(args);

// Get the pushed items
const items = wasArguments(clonedArgs[0])
const items = isArguments(clonedArgs[0])
? [gtagToObj(clonedArgs[0])] // Convert gtag to dataLayer
: clonedArgs; // Regular dataLayer push

items.forEach((obj) => {
// Map the incoming event to a WalkerOS event
const mappedObj = objToEvent(obj, config);
const mappedObj = objToEvent(filterValues(obj), config);

if (mappedObj) {
const { command, event } = mappedObj;
Expand Down
42 changes: 42 additions & 0 deletions packages/utils/src/__tests__/property.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { filterValues, isPropertyType } from '..';

describe('property', () => {
test('isPropertyType', () => {
expect(isPropertyType('string')).toBeTruthy();
expect(isPropertyType(1)).toBeTruthy();
expect(isPropertyType(true)).toBeTruthy();
expect(isPropertyType(undefined)).toBeTruthy();
expect(isPropertyType({})).toBeTruthy();
expect(isPropertyType({ a: '', b: 1, c: true, d: undefined })).toBe(true);
expect(isPropertyType([])).toBeTruthy();
expect(isPropertyType([1, '', true, undefined, {}])).toBeTruthy();

expect(isPropertyType(null)).toBeFalsy();
expect(isPropertyType(NaN)).toBeFalsy();
expect(isPropertyType(Symbol(''))).toBeFalsy();
expect(isPropertyType(() => {})).toBeFalsy();
expect(isPropertyType(new Date())).toBeFalsy();
});

test('filterValues', () => {
expect(filterValues('string')).toBe('string');
expect(filterValues(1)).toBe(1);
expect(filterValues(true)).toBe(true);
expect(filterValues(undefined)).toBe(undefined);
expect(filterValues(null)).toBe(undefined);
expect(filterValues(NaN)).toBe(undefined);
expect(filterValues(Symbol(''))).toBe(undefined);
expect(filterValues(() => {})).toBe(undefined);
expect(filterValues(new Date())).toBe(undefined);
expect(filterValues({})).toStrictEqual({});
expect(
filterValues({
foo: '',
bar: ['', jest.fn()],
a: jest.fn(),
b: new Map(),
c: NaN,
}),
).toStrictEqual({ foo: '', bar: [''] });
});
});

0 comments on commit 80f860e

Please sign in to comment.