Skip to content

Commit

Permalink
Merge pull request #469 from elbwalker/467-data-mapping
Browse files Browse the repository at this point in the history
467 data mapping
  • Loading branch information
alexanderkirtzel authored Dec 5, 2024
2 parents d47ec46 + 2d15fef commit a021161
Show file tree
Hide file tree
Showing 43 changed files with 767 additions and 453 deletions.
14 changes: 14 additions & 0 deletions .changeset/blue-ducks-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@elbwalker/destination-web-google-ads': major
'@elbwalker/destination-web-google-gtm': major
'@elbwalker/destination-web-meta-pixel': major
'@elbwalker/destination-web-plausible': major
'@elbwalker/destination-web-piwikpro': major
'@elbwalker/destination-web-api': major
'@elbwalker/walker.js': minor
'@elbwalker/source-node': minor
'@elbwalker/types': minor
'@elbwalker/utils': minor
---

data mapping [#467](https://github.com/elbwalker/walkerOS/issues/467)
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/destinations/node/etag/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Destination node etag', () => {
event as WalkerOS.Event,
custom ? { custom } : destination.config,
undefined,
instance as SourceNode.Instance,
{ instance: instance as SourceNode.Instance },
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/destinations/web/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"devDependencies": {
"@elbwalker/tsup": "0.0.1",
"@elbwalker/types": "3.1.0"
"@elbwalker/types": "3.1.0",
"@elbwalker/utils": "^3.5.1"
},
"repository": {
"url": "git+https://github.com/elbwalker/walkerOS.git",
Expand Down
22 changes: 21 additions & 1 deletion packages/destinations/web/api/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { DestinationWebAPI } from '.';
describe('Destination API', () => {
const mockSendWeb = jest.fn();
jest.mock('@elbwalker/utils', () => ({
...jest.requireActual('@elbwalker/utils'),
sendWeb: mockSendWeb,
}));

Expand All @@ -15,8 +16,10 @@ describe('Destination API', () => {
function push(
event: WalkerOS.Event,
custom: DestinationWebAPI.Custom = { url },
mapping = {},
options = {},
) {
destination.push(event, { custom });
destination.push(event, { custom }, mapping, options);
}

beforeEach(async () => {
Expand All @@ -39,6 +42,12 @@ describe('Destination API', () => {
);
});

test('fn', () => {
const fn = jest.fn();
destination.push(event, { custom: { url }, fn });
expect(fn).toHaveBeenCalledTimes(1);
});

test('transform', () => {
push(event, { url, transform: () => 'transformed' });
expect(mockSendWeb).toHaveBeenCalledWith(
Expand Down Expand Up @@ -69,4 +78,15 @@ describe('Destination API', () => {
}),
);
});

test('mapping data', () => {
push(event, { url, method: 'POST' }, {}, { data: { foo: 'bar' } });
expect(mockSendWeb).toHaveBeenCalledWith(
url,
JSON.stringify({ foo: 'bar' }),
expect.objectContaining({
method: 'POST',
}),
);
});
});
26 changes: 12 additions & 14 deletions packages/destinations/web/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Destination } from './types';
import { sendWeb } from '@elbwalker/utils';
import type { Custom, Destination } from './types';
import { isDefined, sendWeb } from '@elbwalker/utils';

// Types
export * as DestinationWebAPI from './types';
Expand All @@ -9,22 +9,20 @@ export const destinationWebAPI: Destination = {

config: {},

push(event, config, mapping) {
const {
url,
headers,
method,
transform,
transport = 'fetch',
} = config.custom || {};
push(event, config, mapping, options = {}) {
const { custom = {} as Custom, fn } = config;
const { url, headers, method, transform, transport = 'fetch' } = custom;

if (!url) return;

const data = transform
? transform(event, config, mapping) // Transform event data
: JSON.stringify(event);
const data = isDefined(options.data) ? options.data : event;
const value = Array.isArray(data) ? data[0] : data;
const body = transform
? transform(value, config, mapping) // Transform event data
: JSON.stringify(value);

sendWeb(url, data, { headers, method, transport });
const func = fn || sendWeb;
func(url, body, { headers, method, transport });
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/destinations/web/api/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface Custom {
export interface CustomEvent {}

export type Transform = (
event: WalkerOS.Event,
event?: WalkerOS.Event | WalkerOS.Property,
config?: Config,
mapping?: DestinationWeb.EventMapping<CustomEvent>,
) => SendDataValue;
2 changes: 1 addition & 1 deletion packages/destinations/web/etag/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Destination web etag', () => {
event as WalkerOS.Event,
custom ? { custom } : destination.config,
undefined,
instance as SourceWalkerjs.Instance,
{ instance: instance as SourceWalkerjs.Instance },
);
}

Expand Down
12 changes: 12 additions & 0 deletions packages/destinations/web/google-ads/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ describe('destination Google Ads', () => {
expect(w.gtag).toBeDefined();
});

test('fn', () => {
(w.gtag as unknown) = undefined;
const fn = jest.fn();
elb('walker destination', destination, {
...config,
mapping: { entity: { action: { custom: { label } } } },
fn,
});
elb(event);
expect(fn).toHaveBeenCalledTimes(3);
});

test('Init calls', () => {
elb('walker destination', destination);

Expand Down
18 changes: 12 additions & 6 deletions packages/destinations/web/google-ads/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const destinationGoogleAds: Destination = {
config: {},

init(config) {
const custom = config.custom || {};
const { custom = {}, fn, loadScript } = config;
const w = window;

// required measurement id
Expand All @@ -18,28 +18,33 @@ export const destinationGoogleAds: Destination = {
// Default currency value
custom.currency = custom.currency || 'EUR';

if (config.loadScript) addScript(custom.conversionId);
if (loadScript) addScript(custom.conversionId);

w.dataLayer = w.dataLayer || [];

let func = fn || w.gtag;
if (!w.gtag) {
w.gtag = function gtag() {
// eslint-disable-next-line prefer-rest-params
(w.dataLayer as unknown[]).push(arguments);
};
w.gtag('js', new Date());
func = func || w.gtag;
func('js', new Date());
}

// gtag init call
w.gtag('config', custom.conversionId);
func('config', custom.conversionId);

return config;
},

push(event, config, mapping = {}): void {
const { custom = {}, fn } = config;
const customMapping = mapping.custom;

if (!customMapping) return;

if (!customMapping.label) return;
const custom = config.custom || {};

// Basic conversion parameters
const eventParams: Gtag.CustomParams = {
Expand All @@ -59,7 +64,8 @@ export const destinationGoogleAds: Destination = {
if (customMapping.id)
eventParams.transaction_id = event.data[customMapping.id];

window.gtag('event', 'conversion', eventParams);
const func = fn || window.gtag;
func('event', 'conversion', eventParams);
},
};

Expand Down
3 changes: 2 additions & 1 deletion packages/destinations/web/google-ga4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"devDependencies": {
"@elbwalker/tsup": "0.0.1",
"@elbwalker/types": "3.1.0"
"@elbwalker/types": "3.1.0",
"@elbwalker/utils": "^3.5.1"
},
"repository": {
"url": "git+https://github.com/elbwalker/walkerOS.git",
Expand Down
Loading

0 comments on commit a021161

Please sign in to comment.