Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onboarded XPixel Integration #1783

Merged
merged 20 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const DIR_NAME = 'XPixel';
const NAME = 'XPIXEL';
const DISPLAY_NAME = 'XPixel';

Check warning on line 3 in packages/analytics-js-common/src/constants/integrations/XPixel/constants.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-common/src/constants/integrations/XPixel/constants.js#L1-L3

Added lines #L1 - L3 were not covered by tests

const DISPLAY_NAME_TO_DIR_NAME_MAP = { [DISPLAY_NAME]: DIR_NAME };
const CNameMapping = {

Check warning on line 6 in packages/analytics-js-common/src/constants/integrations/XPixel/constants.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-common/src/constants/integrations/XPixel/constants.js#L5-L6

Added lines #L5 - L6 were not covered by tests
[NAME]: NAME,
XPIXEL: NAME,
XPixel: NAME,
Xpixel: NAME,
xpixel: NAME,
xPixel: NAME,
};

const trackPropertyMapping = [

Check warning on line 15 in packages/analytics-js-common/src/constants/integrations/XPixel/constants.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-common/src/constants/integrations/XPixel/constants.js#L15

Added line #L15 was not covered by tests
{
destKey: 'event_id',
sourceKeys: ['properties.eventId', 'properties.event_id', 'messageId'],
},
{
destKey: 'status',
sourceKeys: 'properties.status',
},
{
destKey: 'currency',
sourceKeys: 'properties.currency',
},
{
destKey: 'value',
sourceKeys: 'properties.value',
},
{
destKey: 'search_string',
sourceKeys: ['properties.search_string', 'properties.searchString'],
},
{
destKey: 'description',
sourceKeys: 'properties.description',
},
{
destKey: 'conversion_id',
sourceKeys: ['properties.conversion_id', 'properties.conversionId'],
},
{
destKey: 'twclid',
sourceKeys: 'properties.twclid',
},
{
destKey: 'contents',
sourceKeys: 'properties.contents',
},
];

export {
NAME,
CNameMapping,
DISPLAY_NAME_TO_DIR_NAME_MAP,
DISPLAY_NAME,
DIR_NAME,
trackPropertyMapping,

Check warning on line 60 in packages/analytics-js-common/src/constants/integrations/XPixel/constants.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-common/src/constants/integrations/XPixel/constants.js#L55-L60

Added lines #L55 - L60 were not covered by tests
};
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const clientToServerNames = {
SPOTIFYPIXEL: 'Spotify Pixel',
COMMANDBAR: 'CommandBar',
NINETAILED: 'Ninetailed',
XPIXEL: 'XPixel',
};

export { clientToServerNames };
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const configToIntNames = {
SPOTIFYPIXEL: 'SpotifyPixel',
COMMANDBAR: 'CommandBar',
NINETAILED: 'Ninetailed',
XPIXEL: 'XPixel',
};

export { configToIntNames };
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
import { CNameMapping as SpotifyPixel } from '../../constants/integrations/SpotifyPixel/constants';
import { CNameMapping as CommandBar } from '../../constants/integrations/CommandBar/constants';
import { CNameMapping as Ninetailed } from '../../constants/integrations/Ninetailed/constants';

import { CNameMapping as XPixel } from '../../constants/integrations/XPixel/constants';

Check warning on line 79 in packages/analytics-js-common/src/v1.1/utils/integration_cname.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-common/src/v1.1/utils/integration_cname.js#L79

Added line #L79 was not covered by tests
// for sdk side native integration identification
// add a mapping from common names to index.js exported key names as identified by Rudder
const commonNames = {
Expand Down Expand Up @@ -159,6 +159,7 @@
...ActiveCampaign,
...Sprig,
...SpotifyPixel,
...XPixel,
};

export { commonNames };
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { XPixel } from '../../../src/integrations/XPixel';

afterAll(() => {
jest.restoreAllMocks();
});
const destinationInfo = {
areTransformationsConnected: false,
destinationId: 'sample-destination-id',
};

const basicConfig = {
pixelId: '12567839',
eventToEventIdMap: [
{ from: 'Sign Up', to: '123' },
{ from: 'Custom', to: '987' },
{ from: 'Page View', to: '456' },
{ from: 'Page View', to: '467' },
{ from: 'product_added', to: '789' },
],
};

describe('XPixel init tests', () => {
beforeAll(() => {
// Add a dummy script as it is required by the init script
const scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.id = 'dummyScript';
const headElements = document.getElementsByTagName('head');
headElements[0].insertBefore(scriptElement, headElements[0].firstChild);
});

let xPixel;

test('Testing init call of XPixel', () => {
xPixel = new XPixel(basicConfig, { logLevel: 'debug' }, destinationInfo);
xPixel.init();
expect(typeof window.twq).toBe('function');
const isReady = xPixel.isReady();
expect(isReady).toBeTruthy();
});
});

describe('xPixel page', () => {
let xPixel;
beforeEach(() => {
xPixel = new XPixel(basicConfig, { logLevel: 'debug' });
xPixel.init();
window.twq = jest.fn();
});

test('send pageview', () => {
xPixel.page({
message: {
context: {},
properties: {
category: 'test cat',
path: '/test',
url: 'http://localhost',
referrer: '',
title: 'test page',
testDimension: 'abc',
value: 35.0,
currency: 'GBP',
},
},
});
expect(window.twq.mock.calls[0]).toEqual(['event', '456', { currency: 'GBP', value: 35 }]);
expect(window.twq.mock.calls[1]).toEqual(['event', '467', { currency: 'GBP', value: 35 }]);
});
});

describe('XPixel Track event', () => {
let xPixel;
beforeEach(() => {});
test('Testing Track Simple Event with contents build properties.products', () => {
xPixel = new XPixel(basicConfig, { logLevel: 'DEBUG' }, destinationInfo);
xPixel.init();
window.twq = jest.fn();
xPixel.track({
message: {
context: {},
event: 'Custom',
properties: {
customProp: 'testProp',
order_id: 'transactionId',
value: 35.0,
coupon: 'APPARELSALE',
currency: 'GBP',
products: [
{
customPropProd: 'testPropProd',
product_id: 'abc',
category: 'Merch',
name: 'Food',
brand: '',
variant: 'Extra topped',
price: 3.0,
quantity: 2,
currency: 'GBP',
position: 1,
value: 6.0,
typeOfProduct: 'Food',
},
],
},
},
});
expect(window.twq.mock.calls[0]).toEqual([
'event',
'987',
{
currency: 'GBP',
value: 35,
contents: [
{
content_type: 'product',
content_id: 'abc',
content_name: 'Food',
price: 3,
num_items: 2,
},
],
},
]);
});

test('Testing Track product_added with contents', () => {
xPixel = new XPixel(basicConfig, { logLevel: 'DEBUG' });
xPixel.init();
window.twq = jest.fn();
xPixel.track({
message: {
context: {},
event: 'product_added',
properties: {
customProp: 'testProp',
event_id: 'purchaseId',
order_id: 'transactionId',
value: 35.0,
currency: 'GBP',
contents: {
content_type: 'product',
content_id: 'abc',
content_name: 'Food/Drink',
price: 3,
num_items: 2,
},
},
},
});
expect(window.twq.mock.calls[0]).toEqual([
'event',
'789',
{
currency: 'GBP',
value: 35,
event_id: 'purchaseId',
contents: [
{
content_type: 'product',
content_id: 'abc',
content_name: 'Food/Drink',
price: 3,
num_items: 2,
},
],
},
]);
});

test('Test for empty properties', () => {
xPixel = new XPixel(basicConfig, { logLevel: 'DEBUG' });
xPixel.init();
window.twq.track = jest.fn();
xPixel.track({
message: {
type: 'track',
context: {},
event: 'Sign Up',
properties: {},
},
});
expect(window.twq.mock.calls[1]).toEqual(['event', '123', {}]);
});

test('Test for no Event name', () => {
xPixel = new XPixel(basicConfig, { logLevel: 'DEBUG' });
xPixel.init();
window.twq.track = jest.fn();
xPixel.track({
message: {
type: 'track',
context: {},
properties: {},
},
});
expect(window.twq).not.toHaveBeenCalledWith();
});

test('Test for no Event Id for input event', () => {
xPixel = new XPixel(basicConfig, { logLevel: 'DEBUG' });
xPixel.init();
window.twq.track = jest.fn();
xPixel.track({
message: {
type: 'track',
context: {},
event: 'addToCart',
properties: {},
},
});
expect(window.twq).not.toHaveBeenCalledWith();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable class-methods-use-this */
import {
NAME,
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/XPixel/constants';
import Logger from '../../utils/logger';
import { getHashFromArrayWithDuplicate } from '../../utils/commonUtils';
import { getTrackResponse } from './utils';
import { loadNativeSdk } from './nativeSdkLoader';

const logger = new Logger(DISPLAY_NAME);
class XPixel {
constructor(config, analytics, destinationInfo) {
if (analytics.logLevel) {
logger.setLogLevel(analytics.logLevel);
}
this.analytics = analytics;
this.pixelId = config.pixelId;
this.eventToEventIdMap = config.eventToEventIdMap;
this.name = NAME;
({
shouldApplyDeviceModeTransformation: this.shouldApplyDeviceModeTransformation,
propagateEventsUntransformedOnError: this.propagateEventsUntransformedOnError,
destinationId: this.destinationId,
} = destinationInfo ?? {});
}

init() {
loadNativeSdk(this.pixelId);
}

isLoaded() {
return !!window.twq;
}

isReady() {
return this.isLoaded();
}

track(rudderElement) {
const { event } = rudderElement.message;
if (!event) {
logger.error('Event name is missing');
return;
}
const properties = getTrackResponse(rudderElement.message);
const standardEventsMap = getHashFromArrayWithDuplicate(this.eventToEventIdMap);
const eventIds = standardEventsMap[event?.toLowerCase()];
if (Array.isArray(eventIds)) {
eventIds.forEach(eventId => {
window.twq('event', eventId, properties);
});
} else {
logger.error(`Event name (${event}) is not valid, must be mapped to atleast one Event ID`);
}
anantjain45823 marked this conversation as resolved.
Show resolved Hide resolved
}

page(rudderElement) {
const event = 'Page View';
const properties = getTrackResponse(rudderElement.message);
const standardEventsMap = getHashFromArrayWithDuplicate(this.eventToEventIdMap);
anantjain45823 marked this conversation as resolved.
Show resolved Hide resolved
const eventIds = standardEventsMap[event?.toLowerCase()];
eventIds.forEach(eventId => {
window.twq('event', eventId, properties);
});
}
}
export default XPixel;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as XPixel } from './browser';
Loading
Loading