Skip to content

Commit

Permalink
feat: add support for getting anonymousId by note attributes array (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna2020 authored Dec 6, 2024
2 parents 481d149 + b3f7140 commit d7f390c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
const lodash = require('lodash');
const get = require('get-value');
// const { RedisError } = require('@rudderstack/integrations-lib');
const stats = require('../../../../util/stats');
const {
getShopifyTopic,
// createPropertiesForEcomEvent,
extractEmailFromPayload,
getAnonymousIdAndSessionId,
// getHashLineItems,
} = require('../../../../v0/sources/shopify/util');
// const logger = require('../../../logger');
const { getShopifyTopic, extractEmailFromPayload } = require('../../../../v0/sources/shopify/util');
const { removeUndefinedAndNullValues, isDefinedAndNotNull } = require('../../../../v0/util');
// const { RedisDB } = require('../../../util/redis/redisConnector');
const Message = require('../../../../v0/sources/message');
const { EventType } = require('../../../../constants');
const {
Expand All @@ -28,6 +19,7 @@ const {
const {
createPropertiesForEcomEventFromWebhook,
getProductsFromLineItems,
getAnonymousIdFromAttributes,
} = require('./serverSideUtlis');

const NO_OPERATION_SUCCESS = {
Expand Down Expand Up @@ -128,12 +120,9 @@ const processEvent = async (inputEvent, metricMetadata) => {
message.setProperty('traits.email', email);
}
}
// attach anonymousId if the event is track event using note_attributes
if (message.type !== EventType.IDENTIFY) {
const { anonymousId } = await getAnonymousIdAndSessionId(
message,
{ shopifyTopic, ...metricMetadata },
null,
);
const anonymousId = getAnonymousIdFromAttributes(event);
if (isDefinedAndNotNull(anonymousId)) {
message.setProperty('anonymousId', anonymousId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
getProductsFromLineItems,
createPropertiesForEcomEventFromWebhook,
getAnonymousIdFromAttributes,
} = require('./serverSideUtlis');

const { constructPayload } = require('../../../../v0/util');
Expand Down Expand Up @@ -109,4 +110,21 @@ describe('serverSideUtils.js', () => {
});
});
});

describe('getAnonymousIdFromAttributes', () => {
// Handles empty note_attributes array gracefully
it('should return null when note_attributes is an empty array', async () => {
const event = { note_attributes: [] };
const result = await getAnonymousIdFromAttributes(event);
expect(result).toBeNull();
});

it('get anonymousId from noteAttributes', async () => {
const event = {
note_attributes: [{ name: 'rudderAnonymousId', value: '123456' }],
};
const result = await getAnonymousIdFromAttributes(event);
expect(result).toEqual('123456');
});
});
});
20 changes: 18 additions & 2 deletions src/v1/sources/shopify/webhookTransformations/serverSideUtlis.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { isDefinedAndNotNull } = require('@rudderstack/integrations-lib');
const { constructPayload } = require('../../../../v0/util');

const {
lineItemsMappingJSON,
productMappingJSON,
Expand All @@ -17,7 +17,6 @@ const getProductsFromLineItems = (lineItems, mapping) => {
}
const products = [];
lineItems.forEach((lineItem) => {
// const product = constructPayload(lineItem, lineItemsMappingJSON);
const product = constructPayload(lineItem, mapping);
products.push(product);
});
Expand All @@ -39,7 +38,24 @@ const createPropertiesForEcomEventFromWebhook = (message) => {
return mappedPayload;
};

/**
* Returns the anonymousId from the noteAttributes array in the webhook event
* @param {Object} event
* @returns {String} anonymousId
*/
const getAnonymousIdFromAttributes = (event) => {
if (!isDefinedAndNotNull(event) || !isDefinedAndNotNull(event.note_attributes)) {
return null; // Return early if event or note_attributes is invalid
}

const noteAttributes = event.note_attributes;
const rudderAnonymousIdObject = noteAttributes.find((attr) => attr.name === 'rudderAnonymousId');

return rudderAnonymousIdObject ? rudderAnonymousIdObject.value : null;
};

module.exports = {
createPropertiesForEcomEventFromWebhook,
getProductsFromLineItems,
getAnonymousIdFromAttributes,
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {

function getNestedValue(object, path) {
const keys = path.split('.');
return keys.reduce((nestedObject, key) => nestedObject && nestedObject[key], object);
return keys.reduce((nestedObject, key) => nestedObject?.[key], object);
}

function setNestedValue(object, path, value) {
Expand Down
15 changes: 15 additions & 0 deletions test/integrations/sources/shopify/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ export const dummyContext = {
},
};

export const note_attributes = [
{
name: 'cartId',
value: '9c623f099fc8819aa4d6a958b65dfe7d',
},
{
name: 'cartToken',
value: 'Z2NwLXVzLWVhc3QxOjAxSkQzNUFXVEI4VkVUNUpTTk1LSzBCMzlF',
},
{
name: 'rudderAnonymousId',
value: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
];

export const responseDummyContext = {
document: {
location: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file contains the test scenarios for the server-side events from the Shopify GraphQL API for
// the v1 transformation flow
import { mockFns } from '../mocks';
import { dummySourceConfig } from '../constants';
import { dummySourceConfig, note_attributes } from '../constants';

export const checkoutEventsTestScenarios = [
{
Expand All @@ -27,7 +26,7 @@ export const checkoutEventsTestScenarios = [
updated_at: '2024-11-05T21:22:02-05:00',
landing_site: '/',
note: '',
note_attributes: [],
note_attributes,
referring_site: '',
shipping_lines: [],
shipping_address: [],
Expand Down Expand Up @@ -145,7 +144,7 @@ export const checkoutEventsTestScenarios = [
updated_at: '2024-11-05T21:22:02-05:00',
landing_site: '/',
note: '',
note_attributes: [],
note_attributes,
referring_site: '',
shipping_lines: [],
shipping_address: [],
Expand Down Expand Up @@ -237,7 +236,7 @@ export const checkoutEventsTestScenarios = [
traits: {
shippingAddress: [],
},
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
],
},
Expand Down Expand Up @@ -269,7 +268,7 @@ export const checkoutEventsTestScenarios = [
created_at: '2024-09-16T03:50:1500:00',
updated_at: '2024-09-17T03:29:02-04:00',
note: '',
note_attributes: [],
note_attributes,
shipping_address: {
first_name: 'testuser',
address1: 'oakwood bridge',
Expand Down Expand Up @@ -396,7 +395,7 @@ export const checkoutEventsTestScenarios = [
output: {
batch: [
{
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
context: {
cart_token: 'Z2NwLXVzLWVhc3QxOjAxSjdXRjdOQjY0NlFFNFdQVEg0MTRFM1E2',
integration: {
Expand All @@ -415,7 +414,7 @@ export const checkoutEventsTestScenarios = [
created_at: '2024-09-16T03:50:1500:00',
updated_at: '2024-09-17T03:29:02-04:00',
note: '',
note_attributes: [],
note_attributes,
shipping_address: {
first_name: 'testuser',
address1: 'oakwood bridge',
Expand Down Expand Up @@ -680,7 +679,7 @@ export const checkoutEventsTestScenarios = [
merchant_of_record_app_id: null,
name: '#1017',
note: null,
note_attributes: [],
note_attributes,
number: 17,
order_number: 1017,
order_status_url:
Expand Down Expand Up @@ -988,7 +987,7 @@ export const checkoutEventsTestScenarios = [
merchant_of_record_app_id: null,
name: '#1017',
note: null,
note_attributes: [],
note_attributes,
number: 17,
order_number: 1017,
order_status_url:
Expand Down Expand Up @@ -1289,7 +1288,7 @@ export const checkoutEventsTestScenarios = [
},
},
timestamp: '2024-11-06T02:54:50.000Z',
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
],
},
Expand Down Expand Up @@ -1326,6 +1325,7 @@ export const checkoutEventsTestScenarios = [
current_total_tax: '0.00',
email: '[email protected]',
name: '#1017',
note_attributes,
order_number: 1017,
order_status_url:
'https://pixel-testing-rs.myshopify.com/59026964593/orders/676613a0027fc8240e16d67fdc9f5ac8/authenticate?key=a70bbe7ec8abcc46b77e4331e4df8c60',
Expand Down Expand Up @@ -1486,6 +1486,7 @@ export const checkoutEventsTestScenarios = [
current_total_tax: '0.00',
email: '[email protected]',
name: '#1017',
note_attributes,
order_number: 1017,
order_status_url:
'https://pixel-testing-rs.myshopify.com/59026964593/orders/676613a0027fc8240e16d67fdc9f5ac8/authenticate?key=a70bbe7ec8abcc46b77e4331e4df8c60',
Expand Down Expand Up @@ -1675,7 +1676,7 @@ export const checkoutEventsTestScenarios = [
},
},
timestamp: '2024-11-06T02:54:50.000Z',
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
],
},
Expand All @@ -1684,4 +1685,4 @@ export const checkoutEventsTestScenarios = [
},
},
},
].map((d1) => ({ ...d1, mockFns }));
];
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file contains the test scenarios for the server-side events from the Shopify GraphQL API for
// the v1 transformation flow
import { mockFns } from '../mocks';
import { dummySourceConfig } from '../constants';
import { dummySourceConfig, note_attributes } from '../constants';

export const genericTrackTestScenarios = [
{
Expand All @@ -24,6 +24,7 @@ export const genericTrackTestScenarios = [
token: 'Z2NwLXVzLWVhc3QxOjAxSjdXRjdOQjY0NlFFNFdQVEg0MTRFM1E2',
line_items: [],
note: '',
note_attributes,
updated_at: '2024-09-17T08:15:13.280Z',
created_at: '2024-09-16T03:50:15.478Z',
},
Expand All @@ -43,7 +44,7 @@ export const genericTrackTestScenarios = [
output: {
batch: [
{
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
context: {
integration: {
name: 'SHOPIFY',
Expand All @@ -58,6 +59,7 @@ export const genericTrackTestScenarios = [
id: 'Z2NwLXVzLWVhc3QxOjAxSjdXRjdOQjY0NlFFNFdQVEg0MTRFM1E2',
line_items: [],
note: '',
note_attributes,
token: 'Z2NwLXVzLWVhc3QxOjAxSjdXRjdOQjY0NlFFNFdQVEg0MTRFM1E2',
updated_at: '2024-09-17T08:15:13.280Z',
},
Expand Down Expand Up @@ -149,7 +151,7 @@ export const genericTrackTestScenarios = [
merchant_of_record_app_id: null,
name: '#1017',
note: null,
note_attributes: [],
note_attributes,
order_number: 1017,
original_total_additional_fees_set: null,
original_total_duties_set: null,
Expand Down Expand Up @@ -363,7 +365,7 @@ export const genericTrackTestScenarios = [
merchant_of_record_app_id: null,
name: '#1017',
note: null,
note_attributes: [],
note_attributes,
order_number: 1017,
original_total_additional_fees_set: null,
original_total_duties_set: null,
Expand Down Expand Up @@ -545,7 +547,7 @@ export const genericTrackTestScenarios = [
traits: {
email: '[email protected]',
},
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '50ead33e-d763-4854-b0ab-765859ef05cb',
},
],
},
Expand Down

0 comments on commit d7f390c

Please sign in to comment.