Skip to content

Commit ec9d576

Browse files
example functions
1 parent 36ea694 commit ec9d576

File tree

5 files changed

+120
-94
lines changed

5 files changed

+120
-94
lines changed
Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,58 @@
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-
];
1+
import type { DestinationGoogleGA4 } from '../src';
2+
import { getEvent } from '@elbwalker/utils';
3+
4+
const customDefault: DestinationGoogleGA4.Custom = {
5+
measurementId: 'G-XXXXXX-1',
6+
};
7+
8+
function useCustom(custom: DestinationGoogleGA4.Custom = customDefault) {
9+
return {
10+
send_to: custom.measurementId,
11+
};
12+
}
13+
14+
export function purchase(custom: DestinationGoogleGA4.Custom = customDefault) {
15+
const event = getEvent('order complete');
16+
const product1 = event.nested[0].data;
17+
const product2 = event.nested[1].data;
18+
19+
return [
20+
'event',
21+
'purchase',
22+
{
23+
transaction_id: event.data.id,
24+
value: event.data.total,
25+
tax: event.data.taxes,
26+
shipping: event.data.shipping,
27+
currency: 'EUR',
28+
items: [
29+
{ item_id: product1.id, item_name: product1.name, quantity: 1 },
30+
{ item_id: product2.id, item_name: product2.name, quantity: 1 },
31+
],
32+
...useCustom(custom),
33+
},
34+
];
35+
}
36+
37+
export function add_to_cart(
38+
custom: DestinationGoogleGA4.Custom = customDefault,
39+
) {
40+
const event = getEvent('product add');
41+
42+
return [
43+
'event',
44+
'add_to_cart',
45+
{
46+
currency: 'EUR',
47+
value: event.data.price,
48+
items: [
49+
{
50+
item_id: event.data.id,
51+
item_variant: event.data.color,
52+
quantity: 1,
53+
},
54+
],
55+
...useCustom(custom),
56+
},
57+
];
58+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * as events from './events';
2-
export { mapping } from './mapping';
2+
export * as mapping from './mapping';
Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
11
import type { Mapping } from '@elbwalker/types';
2+
import type { DestinationGoogleGA4 } from '../src';
23
import { isObject } from '@elbwalker/utils';
34

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-
],
5+
export const purchase: DestinationGoogleGA4.EventConfig = {
6+
name: 'purchase',
7+
data: {
8+
map: {
9+
transaction_id: 'data.id',
10+
value: 'data.total',
11+
tax: 'data.taxes',
12+
shipping: 'data.shipping',
13+
currency: { key: 'data.currency', value: 'EUR' },
14+
items: {
15+
loop: [
16+
'nested',
17+
{
18+
condition: (entity) =>
19+
isObject(entity) && entity.type === 'product',
20+
map: {
21+
item_id: 'data.id',
22+
item_name: 'data.name',
23+
quantity: { key: 'data.quantity', value: 1 },
24+
},
2825
},
29-
},
26+
],
3027
},
3128
},
3229
},
30+
};
31+
32+
export const add_to_cart: DestinationGoogleGA4.EventConfig = {
33+
name: 'add_to_cart',
34+
data: {
35+
map: {
36+
currency: { value: 'EUR', key: 'data.currency' },
37+
override: 'data.old',
38+
value: 'data.price',
39+
items: {
40+
loop: [
41+
'this',
42+
{
43+
map: {
44+
item_id: 'data.id',
45+
item_variant: 'data.color',
46+
quantity: { value: 1, key: 'data.quantity' },
47+
},
48+
},
49+
],
50+
},
51+
},
52+
},
53+
};
54+
55+
export const config = {
56+
order: { complete: purchase },
57+
product: { add: add_to_cart },
3358
} satisfies Mapping.Config;

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

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -299,71 +299,33 @@ describe('Destination Google GA4', () => {
299299

300300
test('event add_to_cart', () => {
301301
const event = getEvent('product add');
302+
const custom = { measurementId, include: [] };
303+
302304
const config: DestinationGoogleGA4.Config = {
303-
custom: { measurementId },
305+
custom,
304306
init: true,
305-
mapping: {
306-
product: {
307-
add: {
308-
name: 'add_to_cart',
309-
data: {
310-
map: {
311-
currency: { value: 'EUR', key: 'data.currency' },
312-
override: 'data.old',
313-
value: 'data.price',
314-
items: {
315-
loop: [
316-
'this',
317-
{
318-
map: {
319-
item_id: 'data.id',
320-
item_variant: 'data.color',
321-
quantity: { value: 1, key: 'data.quantity' },
322-
},
323-
},
324-
],
325-
},
326-
},
327-
},
328-
},
329-
},
330-
},
307+
mapping: mapping.config,
331308
};
332309
elb('walker destination', destination, config);
333310

334311
elb(event);
335312

336-
expect(mockFn).toHaveBeenCalledWith(
337-
'event',
338-
'add_to_cart',
339-
expect.objectContaining({
340-
currency: 'EUR',
341-
value: event.data.price,
342-
items: [
343-
{
344-
item_id: event.data.id,
345-
item_variant: event.data.color,
346-
quantity: 1,
347-
},
348-
],
349-
}),
350-
);
313+
expect(mockFn).toHaveBeenCalledWith(...events.add_to_cart(custom));
351314
});
352315

353316
test('event purchase', () => {
354317
const event = getEvent('order complete');
318+
const custom = { measurementId, include: [] };
355319

356320
const config: DestinationGoogleGA4.Config = {
357-
custom: { measurementId },
321+
custom,
358322
init: true,
359-
mapping,
323+
mapping: mapping.config,
360324
};
361325
elb('walker destination', destination, config);
362326

363327
elb(event);
364-
const product1 = event.nested[0].data;
365-
const product2 = event.nested[1].data;
366-
expect(mockFn).toHaveBeenCalledWith(...events.purchase);
328+
expect(mockFn).toHaveBeenCalledWith(...events.purchase(custom));
367329
});
368330

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface Custom {
2323
transport_url?: string;
2424
}
2525

26+
export type EventConfig = Mapping.EventConfig<CustomEvent>;
27+
2628
export interface CustomEvent {
2729
include?: Include;
2830
}

0 commit comments

Comments
 (0)