Skip to content

Commit

Permalink
custom command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 17, 2024
1 parent 589ee6d commit 3f84690
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/destinations/web/piwikpro/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Destination PiwikPro', () => {
expect(mockFn).toHaveBeenCalledTimes(0);
});

test('event trackEcommerceOrder', () => {
test.skip('event trackEcommerceOrder', () => {

Check warning on line 61 in packages/destinations/web/piwikpro/src/index.test.ts

View workflow job for this annotation

GitHub Actions / Build and test

Disabled test
const order_complete = getEvent('order complete');
elb('walker destination', destination, {
custom,
Expand Down
11 changes: 4 additions & 7 deletions packages/sources/datalayer/src/__tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@ describe('commands', () => {
mapping: {
'set campaign': {
name: 'walker globals',
command: true,
data: {
map: {
data: {
map: {
term: 'term',
},
custom: {
command: {
map: {
term: 'term',
},
},
},
Expand Down
33 changes: 17 additions & 16 deletions packages/sources/datalayer/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { convertConsentStates } from './helper';
const defaultMapping: Mapping = {
'consent default': { ignore: true },
'consent update': {
command: true,
name: 'walker consent',
data: {
map: {
data: {
map: {
// @TODO update list
marketing: 'ad_storage',
analytics: 'analytics_storage',
},
custom: {
command: {
map: {
// @TODO update list
marketing: 'ad_storage',
analytics: 'analytics_storage',
},
},
},
Expand All @@ -34,14 +31,19 @@ export function objToEvent(obj: unknown, config: Config): MappedEvent | void {
...config.mapping,
});

const { command, data, ignore, name } = mapping;
const { custom, data, ignore, name } = mapping;
const eventName = name || `${config.prefix} ${obj.event.replace(/ /g, '_')}`;

if (ignore) return;

// Command
if (custom?.command) {
const data = getMappingValue(obj, custom.command);
return data ? { command: { name: eventName, data } } : undefined;
}

// Mapping values
const values = Array.isArray(data)
? data.map((item) => getMappingValue(event, item))
: getMappingValue(obj, data || {});
const values = getMappingValue(obj, data || {});

// id for duplicate detection
const id = obj.id ? String(obj.id) : getId();
Expand Down Expand Up @@ -78,14 +80,13 @@ export function objToEvent(obj: unknown, config: Config): MappedEvent | void {
}

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

// source type is dataLayer
event.source = event.source ?? {};
event.source.type = event.source.type ?? 'dataLayer';

return { command, event };
return { event };
}

// https://developers.google.com/tag-platform/gtagjs/reference
Expand Down
8 changes: 3 additions & 5 deletions packages/sources/datalayer/src/push.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WalkerOS } from '@elbwalker/types';
import type { Config } from './types';
import { clone, filterValues, isArguments, tryCatch } from '@elbwalker/utils';
import { objToEvent, gtagToObj } from './mapping';
Expand Down Expand Up @@ -32,12 +33,9 @@ export function push(config: Config, ...args: unknown[]) {
if (mappedObj) {
const { command, event } = mappedObj;

if (!event.event) return;

if (command) {
delete event.data?.event;
config.elb(event.event, event.data);
} else {
config.elb(command.name, command.data as WalkerOS.PushData);
} else if (event) {
// Prevent duplicate events
if (config.processedEvents.has(event.id)) return;
config.processedEvents.add(event.id);
Expand Down
20 changes: 14 additions & 6 deletions packages/sources/datalayer/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ export interface Mapping {
[event: string]: EventConfig | undefined;
}

export type EventConfig<CustomEvent = unknown> = Omit<
WalkerOSMapping.EventConfig<CustomEvent>,
export type EventConfig<T = CustomEvent> = Omit<
WalkerOSMapping.EventConfig<T>,
'batch' | 'batchFn' | 'batched' | 'consent'
> & { command?: boolean };
>;

export type MappedEvent =
| { event: WalkerOS.DeepPartialEvent & { id: string }; command?: boolean }
| undefined;
export interface CustomEvent {
command: WalkerOSMapping.Data;
}

export type MappedEvent = {
event?: WalkerOS.DeepPartialEvent & { id: string };
command?: {
name: string;
data: unknown;
};
};

0 comments on commit 3f84690

Please sign in to comment.