Skip to content

Commit

Permalink
WalkerOSMapping.Event
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Nov 15, 2024
1 parent 537223e commit 6ec9415
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 68 deletions.
118 changes: 62 additions & 56 deletions packages/connectors/datalayer/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,38 @@ describe('mapping', () => {
elb,
mapping: {
foo: {
event: { value: 'entity action' },
data: {
some: {
value: 'thing',
custom: {
event: { value: 'entity action' },
data: {
some: {
value: 'thing',
},
key: 'dynamic',
},
context: {
foo: { value: 'bar' },
},
globals: { foo: { value: 'bar' } },
custom: { completely: { value: 'random' } },
user: { hash: { value: 'h4sh' } },
consent: { demo: { value: true } },
id: { value: '1d' },
trigger: { value: 'push' },
entity: { value: 'entity' },
action: { value: 'action' },
timestamp: { value: 1626780000 },
timing: { value: 3.14 },
group: { value: 'group' },
count: { value: 1 },
version: {
client: { value: '0.0.7' },
tagging: { value: 1 },
},
source: {
type: { value: 'test' },
id: { value: 'https://localhost:80' },
previous_id: { value: 'http://remotehost:9001' },
},
key: 'dynamic',
},
context: {
foo: { value: 'bar' },
},
globals: { foo: { value: 'bar' } },
custom: { completely: { value: 'random' } },
user: { hash: { value: 'h4sh' } },
consent: { demo: { value: true } },
id: { value: '1d' },
trigger: { value: 'push' },
entity: { value: 'entity' },
action: { value: 'action' },
timestamp: { value: 1626780000 },
timing: { value: 3.14 },
group: { value: 'group' },
count: { value: 1 },
version: {
client: { value: '0.0.7' },
tagging: { value: 1 },
},
source: {
type: { value: 'test' },
id: { value: 'https://localhost:80' },
previous_id: { value: 'http://remotehost:9001' },
},
},
},
Expand Down Expand Up @@ -238,20 +240,22 @@ describe('mapping', () => {
elb,
mapping: {
add_to_cart: {
event: { value: 'product add' },
data: {
id: 'items.0.item_id',
name: 'items.0.item_name',
discount: 'items.0.discount',
brand: 'items.0.item_brand',
category: 'items.0.item_category',
color: 'items.0.item_variant',
currency: 'currency',
price: 'items.0.price',
quantity: 'items.0.quantity',
total: 'value',
custom: {
event: { value: 'product add' },
data: {
id: 'items.0.item_id',
name: 'items.0.item_name',
discount: 'items.0.discount',
brand: 'items.0.item_brand',
category: 'items.0.item_category',
color: 'items.0.item_variant',
currency: 'currency',
price: 'items.0.price',
quantity: 'items.0.quantity',
total: 'value',
},
// context list
},
// context list
},
},
})!;
Expand Down Expand Up @@ -287,21 +291,23 @@ describe('mapping', () => {
elb,
mapping: {
purchase: {
event: { value: 'order complete' },
data: {
id: 'transaction_id',
currency: 'currency',
shipping: 'shipping',
taxes: 'tax',
total: 'value',
coupon: 'coupon',
},
nested: {
type: { value: 'product' },
custom: {
event: { value: 'order complete' },
data: {
id: 'items.*.item_id',
name: 'items.*.item_name',
price: 'items.*.price',
id: 'transaction_id',
currency: 'currency',
shipping: 'shipping',
taxes: 'tax',
total: 'value',
coupon: 'coupon',
},
nested: {
type: { value: 'product' },
data: {
id: 'items.*.item_id',
name: 'items.*.item_name',
price: 'items.*.price',
},
},
},
},
Expand Down
22 changes: 12 additions & 10 deletions packages/connectors/datalayer/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export function objToEvent(
): (WalkerOS.DeepPartialEvent & { id: string }) | void {
if (!(isObject(obj) && isString(obj.event))) return;

const mapping = config.mapping ? config.mapping[obj.event] : {};
const { custom, ignore, name } = config.mapping?.[obj.event] ?? {};

if (ignore) return; // @TODO test

// id for duplicate detection
const id = obj.id ? String(obj.id) : getId();
Expand All @@ -25,7 +27,7 @@ export function objToEvent(
data: obj as WalkerOS.Properties,
};

if (mapping) {
if (custom) {
const eventMappingValueKeys: Array<keyof EventMappingValues> = [
'event',
'id',
Expand All @@ -39,7 +41,7 @@ export function objToEvent(
];

event = eventMappingValueKeys.reduce((acc, key) => {
const config = mapping[key];
const config = custom[key];
if (config)
(acc as WalkerOS.Properties)[key] = getMappingValue(obj, config);
return acc;
Expand All @@ -56,24 +58,24 @@ export function objToEvent(
];

const objectValues = eventMappingObjectValueKeys.reduce((acc, key) => {
const config = mapping[key];
const config = custom[key];
if (config) acc[key] = mapEntries(obj, config);
return acc;
}, {} as WalkerOS.AnyObject);
event = { ...event, ...objectValues };

if (mapping.context) {
if (custom.context) {
event.context = Object.entries(
mapEntries(obj, mapping.context ?? {}),
mapEntries(obj, custom.context ?? {}),
).reduce((acc, [key, value]) => {
if (value) acc[key] = [value, 0];
return acc;
}, {} as WalkerOS.OrderedProperties);
}

if (mapping.nested) {
if (custom.nested) {
const nested: WalkerOS.Entities = [];
const config = mapping.nested;
const config = custom.nested;

const nestedData = mapEntries(obj, config.data ?? {});
const maxLength = Math.max(
Expand Down Expand Up @@ -106,9 +108,9 @@ export function objToEvent(
}
}

// Update the event name
// Update the event name // @TODO test for name
event.event =
event.event || `${config.prefix} ${obj.event.replace(/ /g, '_')}`;
event.event || name || `${config.prefix} ${obj.event.replace(/ /g, '_')}`;

// source type is dataLayer
event.source = event.source ?? {};
Expand Down
4 changes: 2 additions & 2 deletions packages/connectors/datalayer/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export interface Config {
}

export interface Mapping {
[event: string]: EventMapping;
[event: string]: WalkerOSMapping.Event<Custom>;
}

export type EventMapping = EventMappingValues & EventMappingObjectValues;
export type Custom = EventMappingValues & EventMappingObjectValues;

export type EventMappingObjectValues = {
data?: ObjectValue;
Expand Down

0 comments on commit 6ec9415

Please sign in to comment.