Skip to content

Commit e2f7d89

Browse files
mapping data
1 parent 231a61c commit e2f7d89

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/connectors/datalayer/src/__tests__/mapping.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ describe('mapping', () => {
144144
elb,
145145
mapping: {
146146
foo: {
147-
event: { value: 'page view' },
147+
event: { value: 'entity action' },
148148
data: {
149149
some: {
150150
value: 'thing',
151151
},
152+
key: 'dynamic',
152153
},
153154
},
154155
add_to_cart: {
@@ -161,9 +162,15 @@ describe('mapping', () => {
161162
},
162163
})!;
163164

164-
dataLayer.push({ event: 'foo' });
165+
dataLayer.push({ event: 'foo', dynamic: 'value' });
165166
expect(elb).toHaveBeenCalledWith(
166-
expect.objectContaining({ event: 'page view' }),
167+
expect.objectContaining({
168+
event: 'entity action',
169+
data: {
170+
some: 'thing',
171+
key: 'value',
172+
},
173+
}),
167174
);
168175
});
169176
});

packages/connectors/datalayer/src/mapping.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ export function objToEvent(
1414

1515
// Set default values first
1616

17+
// id for duplicate detection
18+
const id = obj.id ? String(obj.id) : getId();
19+
delete obj.id;
20+
1721
// event name
1822
let event = `${config.prefix} ${obj.event.replace(/ /g, '_')}`;
1923
delete obj.event;
2024

21-
// id for duplicate detection
22-
const id = obj.id ? String(obj.id) : getId();
23-
delete obj.id;
25+
const source = { type: 'dataLayer' } as WalkerOS.Source;
2426

25-
if (mapping) {
26-
if (mapping.event) {
27-
const mappedName = getMappingValue(obj, mapping.event);
28-
if (mappedName) event = String(mappedName);
29-
}
30-
}
27+
let data = obj as WalkerOS.Properties;
3128

32-
const data = obj as WalkerOS.Properties;
29+
if (mapping) {
30+
const mappedName = mapping.event && getMappingValue(obj, mapping.event);
31+
if (mappedName) event = String(mappedName);
3332

34-
const source = { type: 'dataLayer' } as WalkerOS.Source;
33+
if (mapping.data)
34+
data = Object.entries(mapping.data).reduce((acc, [key, value]) => {
35+
acc[key] = getMappingValue(obj, value);
36+
return acc;
37+
}, {} as WalkerOS.Properties);
38+
}
3539

3640
return { event, id, data, source };
3741
}

0 commit comments

Comments
 (0)