Skip to content

Commit 2cd699c

Browse files
command
1 parent eaa2a73 commit 2cd699c

File tree

5 files changed

+92
-16
lines changed

5 files changed

+92
-16
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable prefer-rest-params */
2+
import type { DataLayer } from '../types';
3+
import { connectorDataLayer } from '..';
4+
5+
describe('commands', () => {
6+
const elb = jest.fn(); //.mockImplementation(console.log);
7+
let dataLayer: DataLayer;
8+
9+
const gtag: Gtag.Gtag = function () {
10+
dataLayer.push(arguments);
11+
};
12+
13+
beforeEach(() => {
14+
window.dataLayer = [];
15+
dataLayer = window.dataLayer;
16+
});
17+
18+
test('consent default', () => {
19+
connectorDataLayer({ elb });
20+
21+
gtag('consent', 'default', {
22+
ad_user_data: 'denied',
23+
ad_personalization: 'denied',
24+
ad_storage: 'denied',
25+
analytics_storage: 'denied',
26+
wait_for_update: 500,
27+
});
28+
29+
expect(elb).toHaveBeenCalledTimes(0);
30+
});
31+
32+
test('consent update', () => {
33+
connectorDataLayer({ elb, mapping: { foo: {} } });
34+
35+
gtag('consent', 'update', {
36+
ad_user_data: 'denied',
37+
ad_personalization: 'denied',
38+
ad_storage: 'denied',
39+
analytics_storage: 'granted',
40+
wait_for_update: 500,
41+
});
42+
43+
expect(elb).toHaveBeenNthCalledWith(1, 'walker consent', {
44+
marketing: false,
45+
analytics: true,
46+
});
47+
});
48+
});

packages/connectors/datalayer/src/__tests__/mapping.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ describe('mapping', () => {
152152
elb,
153153
mapping: {
154154
add_to_cart: {
155+
name: 'product add',
155156
custom: {
156-
event: { value: 'product add' },
157157
data: {
158158
id: 'items.0.item_id',
159159
name: 'items.0.item_name',
@@ -203,8 +203,8 @@ describe('mapping', () => {
203203
elb,
204204
mapping: {
205205
purchase: {
206+
name: 'order complete',
206207
custom: {
207-
event: { value: 'order complete' },
208208
data: {
209209
id: 'transaction_id',
210210
currency: 'currency',

packages/connectors/datalayer/src/mapping.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,36 @@ import type {
33
Config,
44
EventMappingObjectValues,
55
EventMappingValues,
6+
MappedEvent,
7+
Mapping,
68
Value,
79
} from './types';
810
import { getId, getMappingValue } from '@elbwalker/utils';
911
import { convertConsentStates, isObject, isString } from './helper';
1012

11-
export function objToEvent(
12-
config: Config,
13-
obj: unknown,
14-
): (WalkerOS.DeepPartialEvent & { id: string }) | void {
13+
const defaultMapping: Mapping = {
14+
'consent default': {
15+
ignore: true,
16+
},
17+
'consent update': {
18+
command: true,
19+
name: 'walker consent',
20+
custom: {
21+
data: {
22+
// @TODO update list
23+
marketing: 'ad_storage',
24+
analytics: 'analytics_storage',
25+
},
26+
},
27+
},
28+
// @TODO set for globals
29+
};
30+
31+
export function objToEvent(config: Config, obj: unknown): MappedEvent | void {
1532
if (!(isObject(obj) && isString(obj.event))) return;
1633

17-
const { custom, ignore, name } = config.mapping?.[obj.event] ?? {};
34+
const mapping = Object.assign(defaultMapping, config.mapping ?? {});
35+
const { command, custom, ignore, name } = mapping[obj.event];
1836

1937
if (ignore) return;
2038

@@ -116,7 +134,7 @@ export function objToEvent(
116134
event.source = event.source ?? {};
117135
event.source.type = event.source.type ?? 'dataLayer';
118136

119-
return event;
137+
return { command, event };
120138
}
121139

122140
function mapEntries(

packages/connectors/datalayer/src/push.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ export function push(config: Config, ...args: unknown[]) {
2828

2929
items.forEach((obj) => {
3030
// Map the incoming event to a WalkerOS event
31-
const event = objToEvent(config, obj);
31+
const mappedObj = objToEvent(config, obj);
3232

33-
if (event) {
34-
// Prevent duplicate events
35-
if (config.processedEvents.has(event.id)) return;
36-
config.processedEvents.add(event.id);
33+
if (mappedObj) {
34+
const { command, event } = mappedObj;
3735

38-
// Hand over to walker instance
39-
config.elb(event);
36+
if (command) {
37+
config.elb(event.event || '', event.data);
38+
} else {
39+
// Prevent duplicate events
40+
if (config.processedEvents.has(event.id)) return;
41+
config.processedEvents.add(event.id);
42+
43+
// Hand over to walker instance
44+
config.elb(event);
45+
}
4046
}
4147
});
4248
},

packages/connectors/datalayer/src/types/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ export interface Config {
1919
export type SupportedMapping<CustomEvent = unknown> = Omit<
2020
WalkerOSMapping.Event<CustomEvent>,
2121
'batch' | 'batchFn' | 'batched' | 'consent'
22-
>;
22+
> & { command?: boolean };
23+
24+
export type MappedEvent =
25+
| { event: WalkerOS.DeepPartialEvent & { id: string }; command?: boolean }
26+
| undefined;
2327

2428
export interface Mapping {
2529
[event: string]: SupportedMapping<Custom>;

0 commit comments

Comments
 (0)