Skip to content

Commit

Permalink
feat: add support for idenitify calls when anonymousid userId both pr…
Browse files Browse the repository at this point in the history
…esent in ecom events (#4092)

* feat: add support for idenitify calls when anonymousid userId both present

* chore: update code, add test

* chore: traits are inside context
  • Loading branch information
yashasvibajpai authored Feb 20, 2025
1 parent e948cef commit b61d1c1
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ const trackPayloadBuilder = (event, shopifyTopic) => {
return message;
};

/**
* Creates an identify event with userId and anonymousId from message object and identifyMappingJSON
* @param {String} message
* @returns {Message} identifyEvent
*/
const createIdentifyEvent = (message) => {
const { userId, anonymousId, traits } = message;
const identifyEvent = new Message(INTEGERATION);
identifyEvent.setEventType(EventType.IDENTIFY);
identifyEvent.userId = userId;
identifyEvent.anonymousId = anonymousId;
const mappedTraits = {};
identifyMappingJSON.forEach((mapping) => {
if (mapping.destKeys.startsWith('traits.')) {
const traitKey = mapping.destKeys.replace('traits.', '');
const sourceValue = get(traits, traitKey);
if (sourceValue !== undefined) {
lodash.set(mappedTraits, traitKey, sourceValue);
}
}
});
// Set the mapped traits
identifyEvent.context.traits = removeUndefinedAndNullValues(mappedTraits);
return identifyEvent;
};

const processEvent = async (inputEvent, metricMetadata) => {
let message;
const event = lodash.cloneDeep(inputEvent);
Expand Down Expand Up @@ -123,6 +149,13 @@ const processEvent = async (inputEvent, metricMetadata) => {
message.userId = String(message.userId);
}
message = removeUndefinedAndNullValues(message);

// if the message payload contains both anonymousId and userId, hence the user is identified
// then create an identify event by multiplexing the original event and return both the message and identify event
if (message.anonymousId && message.userId) {
const identifyEvent = createIdentifyEvent(message);
return [message, identifyEvent];
}
return message;
};
const processWebhookEvents = async (event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,73 @@ export const checkoutEventsTestScenarios = [
type: 'track',
userId: '7188389789809',
},
{
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
context: {
integration: {
name: 'SHOPIFY',
},
library: {
name: 'unknown',
version: 'unknown',
},
traits: {
acceptsMarketing: false,
address: {
address1: 'oakwood bridge',
address2: 'Hedgetown',
city: 'KLF',
company: null,
country: 'United States',
country_code: 'US',
country_name: 'United States',
customer_id: 7188389789809,
default: true,
first_name: 'testuser',
id: null,
last_name: 'dummy',
name: 'testuser dummy',
phone: null,
province: 'Arizona',
province_code: 'AZ',
zip: '85003',
},
adminGraphqlApiId: 'gid://shopify/Customer/7188389789809',
currency: 'USD',
email: '[email protected]',
firstName: 'testuser',
lastName: 'dummy',
orderCount: 0,
shippingAddress: {
address1: 'oakwood bridge',
address2: 'Hedgetown',
city: 'KLF',
company: null,
country: 'United States',
country_code: 'US',
first_name: 'testuser',
last_name: 'dummy',
latitude: null,
longitude: null,
name: 'testuser dummy',
phone: null,
province: 'Arizona',
province_code: 'AZ',
zip: '85003',
},
state: 'disabled',
tags: '',
taxExempt: false,
totalSpent: '0.00',
verifiedEmail: true,
},
},
integrations: {
SHOPIFY: false,
},
type: 'identify',
userId: '7188389789809',
},
],
},
},
Expand Down Expand Up @@ -1302,6 +1369,88 @@ export const checkoutEventsTestScenarios = [
timestamp: '2024-11-06T02:54:50.000Z',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
{
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
context: {
integration: {
name: 'SHOPIFY',
},
library: {
name: 'unknown',
version: 'unknown',
},
traits: {
address: {
address1: 'Yuimaru Kitchen',
address2: '6',
city: 'Johnson City',
company: null,
country: 'United States',
country_code: 'US',
country_name: 'United States',
customer_id: 7358220173425,
default: true,
first_name: 'henry',
id: 8715246862449,
last_name: 'waffles',
name: 'henry waffles',
phone: null,
province: 'Tennessee',
province_code: 'TN',
zip: '37604',
},
adminGraphqlApiId: 'gid://shopify/Customer/7358220173425',
billingAddress: {
address1: 'Yuma Proving Ground',
address2: 'suite 001',
city: 'Yuma Proving Ground',
company: null,
country: 'United States',
country_code: 'US',
first_name: 'yodi',
last_name: 'waffles',
latitude: 33.0177811,
longitude: -114.2525392,
name: 'yodi waffles',
phone: null,
province: 'Arizona',
province_code: 'AZ',
zip: '85365',
},
currency: 'USD',
email: '[email protected]',
firstName: 'yodi',
lastName: 'waffles',
shippingAddress: {
address1: 'Yuimaru Kitchen',
address2: '6',
city: 'Johnson City',
company: null,
country: 'United States',
country_code: 'US',
first_name: 'henry',
last_name: 'waffles',
latitude: 36.3528845,
longitude: -82.4006335,
name: 'henry waffles',
phone: null,
province: 'Tennessee',
province_code: 'TN',
zip: '37604',
},
state: 'disabled',
tags: '',
taxExempt: false,
taxExemptions: [],
verifiedEmail: true,
},
},
integrations: {
SHOPIFY: false,
},
type: 'identify',
userId: '7358220173425',
},
],
},
},
Expand Down Expand Up @@ -1696,6 +1845,85 @@ export const checkoutEventsTestScenarios = [
timestamp: '2024-11-06T02:54:50.000Z',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
{
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
context: {
integration: {
name: 'SHOPIFY',
},
library: {
name: 'unknown',
version: 'unknown',
},
traits: {
address: {
address1: 'Yuimaru Kitchen',
address2: '6',
city: 'Johnson City',
company: null,
country: 'United States',
country_code: 'US',
country_name: 'United States',
customer_id: 7358220173425,
default: true,
first_name: 'henry',
id: 8715246862449,
last_name: 'waffles',
name: 'henry waffles',
phone: null,
province: 'Tennessee',
province_code: 'TN',
zip: '37604',
},
adminGraphqlApiId: 'gid://shopify/Customer/7358220173425',
billingAddress: {
address1: 'Yuma Proving Ground',
address2: 'suite 001',
city: 'Yuma Proving Ground',
company: null,
country: 'United States',
country_code: 'US',
first_name: 'yodi',
last_name: 'waffles',
latitude: 33.0177811,
longitude: -114.2525392,
name: 'yodi waffles',
phone: null,
province: 'Arizona',
province_code: 'AZ',
zip: '85365',
},
currency: 'USD',
email: '[email protected]',
firstName: 'yodi',
lastName: 'waffles',
shippingAddress: {
address1: 'Yuimaru Kitchen',
address2: '6',
city: 'Johnson City',
company: null,
country: 'United States',
country_code: 'US',
first_name: 'henry',
last_name: 'waffles',
latitude: 36.3528845,
longitude: -82.4006335,
name: 'henry waffles',
phone: null,
province: 'Tennessee',
province_code: 'TN',
zip: '37604',
},
state: 'disabled',
taxExemptions: [],
},
},
integrations: {
SHOPIFY: false,
},
type: 'identify',
userId: '7358220173425',
},
],
},
},
Expand Down

0 comments on commit b61d1c1

Please sign in to comment.