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: add support for idenitify calls when anonymousid userId both present in ecom events #4092

Merged
Show file tree
Hide file tree
Changes from 2 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
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.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',
},
},
integrations: {
SHOPIFY: false,
},
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: 'testuser101@gmail.com',
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,
},
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',
},
},
integrations: {
SHOPIFY: false,
},
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: 'henry@wfls.com',
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,
},
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',
},
},
integrations: {
SHOPIFY: false,
},
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: 'henry@wfls.com',
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: [],
},
type: 'identify',
userId: '7358220173425',
},
],
},
},
Expand Down
Loading