Skip to content

Commit 149ac0b

Browse files
examples
1 parent 9899a06 commit 149ac0b

File tree

5 files changed

+151
-18
lines changed

5 files changed

+151
-18
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { WalkerOS } from '@elbwalker/types';
2+
import { getEvent } from '@elbwalker/utils';
3+
4+
export function entity_action(custom: WalkerOS.AnyObject = {}) {
5+
const event = getEvent('entity action');
6+
7+
return {
8+
event: 'entity_action',
9+
data: event.data,
10+
...custom,
11+
};
12+
}
13+
14+
export function purchase(custom: WalkerOS.AnyObject = {}) {
15+
const event = getEvent('order complete');
16+
17+
return {
18+
event: 'purchase',
19+
transaction_id: event.data.id,
20+
value: event.data.total,
21+
tax: event.data.taxes,
22+
shipping: event.data.shipping,
23+
currency: 'EUR',
24+
items: event.nested
25+
.filter((item) => item.type === 'product')
26+
.map((item) => ({
27+
item_id: item.data.id,
28+
item_name: item.data.name,
29+
quantity: 1,
30+
})),
31+
...custom,
32+
};
33+
}
34+
35+
export function add_to_cart(custom: WalkerOS.AnyObject = {}) {
36+
const event = getEvent('product add');
37+
38+
return {
39+
event: 'add_to_cart',
40+
currency: 'EUR',
41+
value: event.data.price,
42+
items: [
43+
{
44+
item_id: event.data.id,
45+
item_variant: event.data.color,
46+
quantity: 1,
47+
},
48+
],
49+
...custom,
50+
};
51+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * as events from './events';
2+
export * as mapping from './mapping';
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { Mapping } from '@elbwalker/types';
2+
import type { DestinationGoogleGTM } from '../src';
3+
import { isObject } from '@elbwalker/utils';
4+
5+
export const entity_action: DestinationGoogleGTM.EventConfig = {
6+
name: 'entity_action',
7+
data: {
8+
map: {
9+
data: 'data',
10+
},
11+
},
12+
};
13+
14+
export const purchase: DestinationGoogleGTM.EventConfig = {
15+
name: 'purchase',
16+
data: {
17+
map: {
18+
transaction_id: 'data.id',
19+
value: 'data.total',
20+
tax: 'data.taxes',
21+
shipping: 'data.shipping',
22+
currency: { key: 'data.currency', value: 'EUR' },
23+
items: {
24+
loop: [
25+
'nested',
26+
{
27+
condition: (entity) =>
28+
isObject(entity) && entity.type === 'product',
29+
map: {
30+
item_id: 'data.id',
31+
item_name: 'data.name',
32+
quantity: { key: 'data.quantity', value: 1 },
33+
},
34+
},
35+
],
36+
},
37+
},
38+
},
39+
};
40+
41+
export const add_to_cart: DestinationGoogleGTM.EventConfig = {
42+
name: 'add_to_cart',
43+
data: {
44+
map: {
45+
currency: { value: 'EUR', key: 'data.currency' },
46+
override: 'data.old',
47+
value: 'data.price',
48+
items: {
49+
loop: [
50+
'this',
51+
{
52+
map: {
53+
item_id: 'data.id',
54+
item_variant: 'data.color',
55+
quantity: { value: 1, key: 'data.quantity' },
56+
},
57+
},
58+
],
59+
},
60+
},
61+
},
62+
};
63+
64+
export const config = {
65+
entity: { action: entity_action },
66+
order: { complete: purchase },
67+
product: { add: add_to_cart },
68+
} satisfies Mapping.Config;

packages/destinations/web/google-gtm/src/index.test.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { WalkerOS } from '@elbwalker/types';
22
import type { DestinationGoogleGTM } from '.';
33
import { mockDataLayer } from '@elbwalker/jest/web.setup';
44
import { elb, Walkerjs } from '@elbwalker/walker.js';
5-
import { createEvent } from '@elbwalker/utils';
5+
import { createEvent, getEvent } from '@elbwalker/utils';
6+
import { events, mapping } from '../examples';
67

78
describe('destination google-tag-manager', () => {
89
const w = window;
@@ -81,23 +82,27 @@ describe('destination google-tag-manager', () => {
8182
expect(mockDataLayer).toHaveBeenLastCalledWith(event);
8283
});
8384

84-
test('push mapping data', () => {
85-
elb('walker destination', destination, {
86-
mapping: {
87-
entity: {
88-
action: {
89-
data: {
90-
map: {
91-
foo: { value: 'bar' },
92-
},
93-
},
94-
},
95-
},
96-
},
97-
});
85+
test('event entity_action', () => {
86+
const event = getEvent();
87+
elb('walker destination', destination, { mapping: mapping.config });
88+
elb(event);
89+
90+
expect(mockDataLayer).toHaveBeenLastCalledWith(events.entity_action());
91+
});
92+
93+
test('event add_to_cart', () => {
94+
const event = getEvent('product add');
95+
elb('walker destination', destination, { mapping: mapping.config });
96+
elb(event);
97+
98+
expect(mockDataLayer).toHaveBeenLastCalledWith(events.add_to_cart());
99+
});
100+
101+
test('event purchase', () => {
102+
const event = getEvent('order complete');
103+
elb('walker destination', destination, { mapping: mapping.config });
98104
elb(event);
99-
expect(w.dataLayer).toBeDefined();
100105

101-
expect(mockDataLayer).toHaveBeenLastCalledWith({ foo: 'bar' });
106+
expect(mockDataLayer).toHaveBeenLastCalledWith(events.purchase());
102107
});
103108
});

packages/destinations/web/google-gtm/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Custom, Destination } from './types';
2+
import { isObject } from '@elbwalker/utils';
23

34
const defaultDataLayer = 'dataLayer';
45
const defaultDomain = 'https://www.googletagmanager.com/gtm.js?id=';
@@ -32,7 +33,13 @@ export const destinationGoogleGTM: Destination = {
3233

3334
push(event, config, mapping, options = {}) {
3435
const func = config.fn || (window.dataLayer as unknown[]).push;
35-
func(options.data ?? event);
36+
const { data } = options;
37+
const obj = { event: event.event }; // Use the name mapping by default
38+
39+
func({
40+
...obj,
41+
...(isObject(data) ? data : event),
42+
});
3643
},
3744
};
3845

0 commit comments

Comments
 (0)