Skip to content

Commit 36ea694

Browse files
event and mapping examples
1 parent 9dd0024 commit 36ea694

File tree

4 files changed

+61
-45
lines changed

4 files changed

+61
-45
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const purchase = [
2+
'event',
3+
'purchase',
4+
{
5+
data_id: '0rd3r1d',
6+
data_currency: 'EUR',
7+
data_shipping: 5.22,
8+
data_taxes: 73.76,
9+
data_total: 555,
10+
transaction_id: '0rd3r1d',
11+
value: 555,
12+
tax: 73.76,
13+
shipping: 5.22,
14+
currency: 'EUR',
15+
items: [
16+
{ item_id: 'ers', item_name: 'Everyday Ruck Snack', quantity: 1 },
17+
{ item_id: 'cc', item_name: 'Cool Cap', quantity: 1 },
18+
],
19+
send_to: 'G-XXXXXX-1',
20+
},
21+
];
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 { mapping } from './mapping';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { Mapping } from '@elbwalker/types';
2+
import { isObject } from '@elbwalker/utils';
3+
4+
export const mapping = {
5+
order: {
6+
complete: {
7+
name: 'purchase',
8+
data: {
9+
map: {
10+
transaction_id: 'data.id',
11+
value: 'data.total',
12+
tax: 'data.taxes',
13+
shipping: 'data.shipping',
14+
currency: { key: 'data.currency', value: 'EUR' },
15+
items: {
16+
loop: [
17+
'nested',
18+
{
19+
condition: (entity) =>
20+
isObject(entity) && entity.type === 'product',
21+
map: {
22+
item_id: 'data.id',
23+
item_name: 'data.name',
24+
quantity: { key: 'data.quantity', value: 1 },
25+
},
26+
},
27+
],
28+
},
29+
},
30+
},
31+
},
32+
},
33+
} satisfies Mapping.Config;

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

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { getEvent } from '@elbwalker/utils';
21
import type { DestinationGoogleGA4 } from '.';
2+
import { getEvent, isObject } from '@elbwalker/utils';
33
import { elb, Walkerjs } from '@elbwalker/walker.js';
4+
import { events, mapping } from '../examples';
45

56
describe('Destination Google GA4', () => {
67
const w = window;
@@ -351,59 +352,18 @@ describe('Destination Google GA4', () => {
351352

352353
test('event purchase', () => {
353354
const event = getEvent('order complete');
355+
354356
const config: DestinationGoogleGA4.Config = {
355357
custom: { measurementId },
356358
init: true,
357-
mapping: {
358-
order: {
359-
complete: {
360-
name: 'purchase',
361-
data: {
362-
map: {
363-
transaction_id: 'data.id',
364-
value: 'data.total',
365-
tax: 'data.taxes',
366-
shipping: 'data.shipping',
367-
currency: { key: 'data.currency', value: 'EUR' },
368-
items: {
369-
loop: [
370-
'nested',
371-
{
372-
condition: (entity) => entity.type === 'product',
373-
map: {
374-
item_id: 'data.id',
375-
item_name: 'data.name',
376-
quantity: { key: 'data.quantity', value: 1 },
377-
},
378-
},
379-
],
380-
},
381-
},
382-
},
383-
},
384-
},
385-
},
359+
mapping,
386360
};
387361
elb('walker destination', destination, config);
388362

389363
elb(event);
390364
const product1 = event.nested[0].data;
391365
const product2 = event.nested[1].data;
392-
expect(mockFn).toHaveBeenCalledWith(
393-
'event',
394-
'purchase',
395-
expect.objectContaining({
396-
transaction_id: event.data.id,
397-
value: event.data.total,
398-
tax: event.data.taxes,
399-
shipping: event.data.shipping,
400-
currency: 'EUR',
401-
items: [
402-
{ item_id: product1.id, item_name: product1.name, quantity: 1 },
403-
{ item_id: product2.id, item_name: product2.name, quantity: 1 },
404-
],
405-
}),
406-
);
366+
expect(mockFn).toHaveBeenCalledWith(...events.purchase);
407367
});
408368

409369
test('snake case disabled', () => {

0 commit comments

Comments
 (0)