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

fix: Support capturing click ids as custom flags in commerce events #939

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion src/ecommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,10 @@ export default function Ecommerce(mpInstance) {

baseEvent.CurrencyCode = mpInstance._Store.currencyCode;
baseEvent.ShoppingCart = [];
baseEvent.CustomFlags = customFlags;
baseEvent.CustomFlags = {
...baseEvent.CustomFlags,
...customFlags,
};

return baseEvent;
} else {
Expand Down
2 changes: 2 additions & 0 deletions test/src/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ var pluses = /\+/g,
if (batch.events.length) {
return batch.events.find(function(event) {
switch (event.event_type) {
case 'screen_view':
return event.data.screen_name === eventName;
case 'commerce_event':
if (event.data.product_action) {
return event.data.product_action.action === eventName;
Expand Down
201 changes: 201 additions & 0 deletions test/src/tests-integration-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,205 @@ describe('Integration Capture', () => {
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to event custom flags, prioritizing passed in custom flags', async () => {
await waitForCondition(hasIdentifyReturned);
window.mParticle.logEvent(
'Test Event',
mParticle.EventType.Navigation,
{ mykey: 'myvalue' },
{ 'Facebook.ClickId': 'passed-in' },
);

const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event');


expect(testEvent).to.have.property('data');
expect(testEvent.data).to.have.property('event_name', 'Test Event');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
'Facebook.ClickId': 'passed-in',
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to page view custom flags', async () => {
await waitForCondition(hasIdentifyReturned);

window.mParticle.logPageView(
'Test Page View',
{'foo-attr': 'bar-attr'}
);

const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Page View');

const initialTimestamp = window.mParticle.getInstance()._IntegrationCapture.initialTimestamp;

expect(testEvent).to.have.property('data');
expect(testEvent.data).to.have.property('screen_name', 'Test Page View');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
'Facebook.ClickId': `fb.1.${initialTimestamp}.1234`,
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to page view custom flags, prioritizing passed in custom flags', async () => {
await waitForCondition(hasIdentifyReturned);
window.mParticle.logPageView(
'Test Page View',
{'foo-attr': 'bar-attr'},
{'Facebook.ClickId': 'passed-in'},
);

const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Page View');

expect(testEvent).to.have.property('data');
expect(testEvent.data).to.have.property('screen_name', 'Test Page View');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
'Facebook.ClickId': 'passed-in',
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to commerce event custom flags', async () => {
await waitForCondition(hasIdentifyReturned);

const product1 = mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999, 1);
const product2 = mParticle.eCommerce.createProduct('galaxy', 'galaxySKU', 799, 1);

const transactionAttributes = {
Id: 'foo-transaction-id',
Revenue: 430.00,
Tax: 30
};

const customAttributes = {sale: true};
const customFlags = {foo: 'bar'};

mParticle.eCommerce.logProductAction(
mParticle.ProductActionType.Purchase,
[product1, product2],
customAttributes,
customFlags,
transactionAttributes);

const testEvent = findEventFromRequest(fetchMock.calls(), 'purchase');

const initialTimestamp = window.mParticle.getInstance()._IntegrationCapture.initialTimestamp;

expect(testEvent.data.product_action).to.have.property('action', 'purchase');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
foo: 'bar',
'Facebook.ClickId': `fb.1.${initialTimestamp}.1234`,
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to commerce event custom flags, prioritizing passed in flags', async () => {
await waitForCondition(hasIdentifyReturned);

const product1 = mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999, 1);
const product2 = mParticle.eCommerce.createProduct('galaxy', 'galaxySKU', 799, 1);

const transactionAttributes = {
Id: 'foo-transaction-id',
Revenue: 430.00,
Tax: 30
};

const customAttributes = {sale: true};
const customFlags = {
'Facebook.ClickId': 'passed-in'
};

mParticle.eCommerce.logProductAction(
mParticle.ProductActionType.Purchase,
[product1, product2],
customAttributes,
customFlags,
transactionAttributes);


const testEvent = findEventFromRequest(fetchMock.calls(), 'purchase');

expect(testEvent.data.product_action).to.have.property('action', 'purchase');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
'Facebook.ClickId': 'passed-in',
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to commerce event custom flags', async () => {
await waitForCondition(hasIdentifyReturned);

const product1 = mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999, 1);
const product2 = mParticle.eCommerce.createProduct('galaxy', 'galaxySKU', 799, 1);

const transactionAttributes = {
Id: 'foo-transaction-id',
Revenue: 430.00,
Tax: 30
};

const customAttributes = {sale: true};
const customFlags = {foo: 'bar'};

mParticle.eCommerce.logProductAction(
mParticle.ProductActionType.Purchase,
[product1, product2],
customAttributes,
customFlags,
transactionAttributes);

const testEvent = findEventFromRequest(fetchMock.calls(), 'purchase');

const initialTimestamp = window.mParticle.getInstance()._IntegrationCapture.initialTimestamp;

expect(testEvent.data.product_action).to.have.property('action', 'purchase');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
foo: 'bar',
'Facebook.ClickId': `fb.1.${initialTimestamp}.1234`,
'Facebook.BrowserId': '54321',
});
});

it('should add captured integrations to commerce event custom flags, prioritizing passed in flags', async () => {
await waitForCondition(hasIdentifyReturned);

const product1 = mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999, 1);
const product2 = mParticle.eCommerce.createProduct('galaxy', 'galaxySKU', 799, 1);

const transactionAttributes = {
Id: 'foo-transaction-id',
Revenue: 430.00,
Tax: 30
};

const customAttributes = {sale: true};
const customFlags = {
'Facebook.ClickId': 'passed-in'
};

mParticle.eCommerce.logProductAction(
mParticle.ProductActionType.Purchase,
[product1, product2],
customAttributes,
customFlags,
transactionAttributes);


const testEvent = findEventFromRequest(fetchMock.calls(), 'purchase');

expect(testEvent.data.product_action).to.have.property('action', 'purchase');
expect(testEvent.data).to.have.property('custom_flags');
expect(testEvent.data.custom_flags).to.deep.equal({
'Facebook.ClickId': 'passed-in',
'Facebook.BrowserId': '54321',
});
});
});
Loading