Skip to content

Commit

Permalink
command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Nov 15, 2024
1 parent eaa2a73 commit 2cd699c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 16 deletions.
48 changes: 48 additions & 0 deletions packages/connectors/datalayer/src/__tests__/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable prefer-rest-params */
import type { DataLayer } from '../types';
import { connectorDataLayer } from '..';

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

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

beforeEach(() => {
window.dataLayer = [];
dataLayer = window.dataLayer;
});

test('consent default', () => {
connectorDataLayer({ elb });

gtag('consent', 'default', {
ad_user_data: 'denied',
ad_personalization: 'denied',
ad_storage: 'denied',
analytics_storage: 'denied',
wait_for_update: 500,
});

expect(elb).toHaveBeenCalledTimes(0);
});

test('consent update', () => {
connectorDataLayer({ elb, mapping: { foo: {} } });

gtag('consent', 'update', {
ad_user_data: 'denied',
ad_personalization: 'denied',
ad_storage: 'denied',
analytics_storage: 'granted',
wait_for_update: 500,
});

expect(elb).toHaveBeenNthCalledWith(1, 'walker consent', {
marketing: false,
analytics: true,
});
});
});
4 changes: 2 additions & 2 deletions packages/connectors/datalayer/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ describe('mapping', () => {
elb,
mapping: {
add_to_cart: {
name: 'product add',
custom: {
event: { value: 'product add' },
data: {
id: 'items.0.item_id',
name: 'items.0.item_name',
Expand Down Expand Up @@ -203,8 +203,8 @@ describe('mapping', () => {
elb,
mapping: {
purchase: {
name: 'order complete',
custom: {
event: { value: 'order complete' },
data: {
id: 'transaction_id',
currency: 'currency',
Expand Down
30 changes: 24 additions & 6 deletions packages/connectors/datalayer/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@ import type {
Config,
EventMappingObjectValues,
EventMappingValues,
MappedEvent,
Mapping,
Value,
} from './types';
import { getId, getMappingValue } from '@elbwalker/utils';
import { convertConsentStates, isObject, isString } from './helper';

export function objToEvent(
config: Config,
obj: unknown,
): (WalkerOS.DeepPartialEvent & { id: string }) | void {
const defaultMapping: Mapping = {
'consent default': {
ignore: true,
},
'consent update': {
command: true,
name: 'walker consent',
custom: {
data: {
// @TODO update list
marketing: 'ad_storage',
analytics: 'analytics_storage',
},
},
},
// @TODO set for globals
};

export function objToEvent(config: Config, obj: unknown): MappedEvent | void {
if (!(isObject(obj) && isString(obj.event))) return;

const { custom, ignore, name } = config.mapping?.[obj.event] ?? {};
const mapping = Object.assign(defaultMapping, config.mapping ?? {});
const { command, custom, ignore, name } = mapping[obj.event];

if (ignore) return;

Expand Down Expand Up @@ -116,7 +134,7 @@ export function objToEvent(
event.source = event.source ?? {};
event.source.type = event.source.type ?? 'dataLayer';

return event;
return { command, event };
}

function mapEntries(
Expand Down
20 changes: 13 additions & 7 deletions packages/connectors/datalayer/src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ export function push(config: Config, ...args: unknown[]) {

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

if (event) {
// Prevent duplicate events
if (config.processedEvents.has(event.id)) return;
config.processedEvents.add(event.id);
if (mappedObj) {
const { command, event } = mappedObj;

// Hand over to walker instance
config.elb(event);
if (command) {
config.elb(event.event || '', event.data);
} else {
// Prevent duplicate events
if (config.processedEvents.has(event.id)) return;
config.processedEvents.add(event.id);

// Hand over to walker instance
config.elb(event);
}
}
});
},
Expand Down
6 changes: 5 additions & 1 deletion packages/connectors/datalayer/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export interface Config {
export type SupportedMapping<CustomEvent = unknown> = Omit<
WalkerOSMapping.Event<CustomEvent>,
'batch' | 'batchFn' | 'batched' | 'consent'
>;
> & { command?: boolean };

export type MappedEvent =
| { event: WalkerOS.DeepPartialEvent & { id: string }; command?: boolean }
| undefined;

export interface Mapping {
[event: string]: SupportedMapping<Custom>;
Expand Down

0 comments on commit 2cd699c

Please sign in to comment.