Skip to content

Commit

Permalink
event and mapping examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Feb 17, 2025
1 parent 9dd0024 commit 36ea694
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
21 changes: 21 additions & 0 deletions packages/destinations/web/google-ga4/examples/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const purchase = [
'event',
'purchase',
{
data_id: '0rd3r1d',
data_currency: 'EUR',
data_shipping: 5.22,
data_taxes: 73.76,
data_total: 555,
transaction_id: '0rd3r1d',
value: 555,
tax: 73.76,
shipping: 5.22,
currency: 'EUR',
items: [
{ item_id: 'ers', item_name: 'Everyday Ruck Snack', quantity: 1 },
{ item_id: 'cc', item_name: 'Cool Cap', quantity: 1 },
],
send_to: 'G-XXXXXX-1',
},
];
2 changes: 2 additions & 0 deletions packages/destinations/web/google-ga4/examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as events from './events';
export { mapping } from './mapping';
33 changes: 33 additions & 0 deletions packages/destinations/web/google-ga4/examples/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Mapping } from '@elbwalker/types';
import { isObject } from '@elbwalker/utils';

export const mapping = {
order: {
complete: {
name: 'purchase',
data: {
map: {
transaction_id: 'data.id',
value: 'data.total',
tax: 'data.taxes',
shipping: 'data.shipping',
currency: { key: 'data.currency', value: 'EUR' },
items: {
loop: [
'nested',
{
condition: (entity) =>
isObject(entity) && entity.type === 'product',
map: {
item_id: 'data.id',
item_name: 'data.name',
quantity: { key: 'data.quantity', value: 1 },
},
},
],
},
},
},
},
},
} satisfies Mapping.Config;
50 changes: 5 additions & 45 deletions packages/destinations/web/google-ga4/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getEvent } from '@elbwalker/utils';
import type { DestinationGoogleGA4 } from '.';
import { getEvent, isObject } from '@elbwalker/utils';
import { elb, Walkerjs } from '@elbwalker/walker.js';
import { events, mapping } from '../examples';

describe('Destination Google GA4', () => {
const w = window;
Expand Down Expand Up @@ -351,59 +352,18 @@ describe('Destination Google GA4', () => {

test('event purchase', () => {
const event = getEvent('order complete');

const config: DestinationGoogleGA4.Config = {
custom: { measurementId },
init: true,
mapping: {
order: {
complete: {
name: 'purchase',
data: {
map: {
transaction_id: 'data.id',
value: 'data.total',
tax: 'data.taxes',
shipping: 'data.shipping',
currency: { key: 'data.currency', value: 'EUR' },
items: {
loop: [
'nested',
{
condition: (entity) => entity.type === 'product',
map: {
item_id: 'data.id',
item_name: 'data.name',
quantity: { key: 'data.quantity', value: 1 },
},
},
],
},
},
},
},
},
},
mapping,
};
elb('walker destination', destination, config);

elb(event);
const product1 = event.nested[0].data;
const product2 = event.nested[1].data;
expect(mockFn).toHaveBeenCalledWith(
'event',
'purchase',
expect.objectContaining({
transaction_id: event.data.id,
value: event.data.total,
tax: event.data.taxes,
shipping: event.data.shipping,
currency: 'EUR',
items: [
{ item_id: product1.id, item_name: product1.name, quantity: 1 },
{ item_id: product2.id, item_name: product2.name, quantity: 1 },
],
}),
);
expect(mockFn).toHaveBeenCalledWith(...events.purchase);
});

test('snake case disabled', () => {
Expand Down

0 comments on commit 36ea694

Please sign in to comment.