Skip to content

Commit

Permalink
fix: algolia enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Apr 17, 2024
1 parent 8592e66 commit a140e2d
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 5 deletions.
32 changes: 31 additions & 1 deletion src/cdk/v2/destinations/algolia/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ bindings:
- path: ../../../../v0/destinations/algolia/config
- name: removeUndefinedAndNullValues
path: ../../../../v0/util
- name: isDefinedAndNotNull
path: ../../../../v0/util
- path: ../../bindings/jsontemplate
- path: '@rudderstack/integrations-lib'

steps:
- name: validateInput
Expand All @@ -24,6 +27,10 @@ steps:
let eventTypeMap = $.eventTypeMapping(.destination.Config);
let event = .message.event.trim().toLowerCase();
let eventType = .message.properties.eventType ?? eventTypeMap[event];
let eventSubType = .message.properties.eventSubtype && eventType === 'conversion' ?
.message.properties.eventSubtype in $.ALLOWED_EVENT_SUBTYPES ?
.message.properties.eventSubtype : null
: null;
$.assert(eventType, "eventType is mandatory for track call");
let payload = .message.().({
index: .properties.index,
Expand All @@ -32,12 +39,33 @@ steps:
filters: .properties.filters,
objectIDs: .properties.objectIds,
positions: .properties.positions,
value: $.isDefinedAndNotNull(.properties.currency) ? .properties.value : null,
currency: .properties.currency,
userToken: {{{{$.getGenericPaths("userId", "||")}}}},
eventName: event,
eventType: eventType
eventType: eventType,
eventSubtype: eventSubType
});
$.context.payload = $.genericpayloadValidator(payload);
- name: prepareObjectDataBlock
condition: $.context.payload.eventType === "conversion"
description: |
Populate list of objectData
template: |
const populateObjectData = function(singleProduct) {
singleProduct.({
"queryID" : $.isDefinedAndNotNull(.queryID) ? String(.queryID) : null,
"price": $.isDefinedAndNotNull(.price) && $.isDefinedAndNotNull(^.message.properties.currency) ? String(.price) : null,
"quantity": $.isDefinedAndNotNull(.quantity)? Number(.quantity) : null,
"discount": $.isDefinedAndNotNull(.discount) ? String(.discount) : null
})
};
const products = .message.properties.products
products.map(function(singleProduct) {
$.removeUndefinedAndNullValues(populateObjectData(singleProduct))
})[]
- name: populateProductsData
condition: |
.message.properties.products &&
Expand All @@ -55,11 +83,13 @@ steps:
const products = .message.properties.products;
const objectIDs = ~r products.objectId;
$.context.payload.objectIDs = Array.isArray(objectIDs) ? objectIDs[:20]:$.context.payload.objectIDs;
$.context.payload.objectData = $.outputs.prepareObjectDataBlock
- name: validateDestPayload
template: |
const filters = $.context.payload.filters;
const objectIDs = $.context.payload.objectIDs;
const objectData = $.context.payload.objectData;
$.assert(!(filters && objectIDs), "event can't have both objectIds and filters at the same time.");
$.assert(filters.length || objectIDs.length, "Either filters or objectIds is required and must be non empty.");
Expand Down
2 changes: 2 additions & 0 deletions src/v0/destinations/algolia/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const CONFIG_CATEGORIES = {
TRACK: { type: 'track', name: 'AlgoliaTrack' },
};
const EVENT_TYPES = ['click', 'view', 'conversion'];
const ALLOWED_EVENT_SUBTYPES = ['addToCart', 'purchase'];
const MAX_BATCH_SIZE = 1000;
const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname);
module.exports = {
ENDPOINT,
MAX_BATCH_SIZE,
EVENT_TYPES,
trackMapping: MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK.name],
ALLOWED_EVENT_SUBTYPES,
};
10 changes: 10 additions & 0 deletions src/v0/destinations/algolia/data/AlgoliaTrack.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@
"destKey": "positions",
"sourceKeys": "properties.positions",
"required": false
},
{
"destKey": "value",
"sourceKeys": "properties.value",
"required": false
},
{
"destKey": "currency",
"sourceKeys": "properties.currency",
"required": false
}
]
24 changes: 22 additions & 2 deletions src/v0/destinations/algolia/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
handleRtTfSingleEventError,
} = require('../../util/index');

const { ENDPOINT, MAX_BATCH_SIZE, trackMapping } = require('./config');
const { ENDPOINT, MAX_BATCH_SIZE, trackMapping, ALLOWED_EVENT_SUBTYPES } = require('./config');

const {
genericpayloadValidator,
Expand All @@ -38,6 +38,12 @@ const trackResponseBuilder = (message, { Config }) => {
const eventMapping = eventTypeMapping(Config);
payload.eventName = event;
payload.eventType = getValueFromMessage(message, 'properties.eventType') || eventMapping[event];
if (
payload.eventType === 'conversion' &&
ALLOWED_EVENT_SUBTYPES.includes(getValueFromMessage(message, 'properties.eventSubtype'))

Check warning on line 43 in src/v0/destinations/algolia/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/transform.js#L43

Added line #L43 was not covered by tests
) {
payload.eventSubtype = getValueFromMessage(message, 'properties.eventSubtype');

Check warning on line 45 in src/v0/destinations/algolia/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/transform.js#L45

Added line #L45 was not covered by tests
}

if (!payload.eventType) {
throw new InstrumentationError('eventType is mandatory for track call');
Expand All @@ -47,9 +53,13 @@ const trackResponseBuilder = (message, { Config }) => {
if (event === 'product list viewed' || event === 'order completed') {
const products = getValueFromMessage(message, 'properties.products');
if (products) {
const { objectList, positionList } = createObjectArray(products, payload.eventType);
const { objectList, positionList, objectData } = createObjectArray(
products,
payload.eventType,
);
const objLen = objectList.length;
const posLen = positionList.length;
const objDataLen = objectData.length;
if (objLen > 0) {
payload.objectIDs = objectList;
payload.objectIDs.splice(20);
Expand All @@ -58,10 +68,20 @@ const trackResponseBuilder = (message, { Config }) => {
payload.positions = positionList;
payload.positions.splice(20);
}

if (objDataLen > 0) {
payload.objectData = objectData;

Check warning on line 73 in src/v0/destinations/algolia/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/transform.js#L73

Added line #L73 was not covered by tests
}
// making size of object list and position list equal
if (posLen > 0 && objLen > 0 && posLen !== objLen) {
throw new InstrumentationError('length of objectId and position should be equal');
}

if (objDataLen > 0 && objLen > 0 && objDataLen !== objLen) {
throw new InstrumentationError(

Check warning on line 81 in src/v0/destinations/algolia/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/transform.js#L81

Added line #L81 was not covered by tests
'length of objectId and properties.products array should be equal',
);
}
}
}
// for all events either filter or objectID should be there
Expand Down
19 changes: 17 additions & 2 deletions src/v0/destinations/algolia/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const {
InstrumentationError,
isDefined,
removeUndefinedAndNullValues,
} = require('@rudderstack/integrations-lib');
const logger = require('../../../logger');
const { EVENT_TYPES } = require('./config');

Expand Down Expand Up @@ -66,6 +70,8 @@ const genericpayloadValidator = (payload) => {
const createObjectArray = (objects, eventType) => {
const objectList = [];
const positionList = [];
// eslint-disable-next-line sonarjs/no-unused-collection
const objectData = [];
if (objects.length > 0) {
objects.forEach((object, index) => {
if (object.objectId) {
Expand All @@ -80,13 +86,22 @@ const createObjectArray = (objects, eventType) => {
}
} else {
objectList.push(object.objectId);
if (eventType === 'conversion') {
const singleObjData = {
queryID: isDefined(object.queryID) ? `${object.queryID}` : null,
price: isDefined(object.price) ? `${object.price}` : null,

Check warning on line 92 in src/v0/destinations/algolia/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/util.js#L90-L92

Added lines #L90 - L92 were not covered by tests
quantity: object.quantity,
discount: isDefined(object.discount) ? `${object.discount}` : null,

Check warning on line 94 in src/v0/destinations/algolia/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/util.js#L94

Added line #L94 was not covered by tests
};
objectData.push(removeUndefinedAndNullValues(singleObjData));

Check warning on line 96 in src/v0/destinations/algolia/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/algolia/util.js#L96

Added line #L96 was not covered by tests
}
}
} else {
logger.error(`object at index ${index} dropped. objectId is required.`);
}
});
}
return { objectList, positionList };
return { objectList, positionList, objectData };
};

const clickPayloadValidator = (payload) => {
Expand Down
Loading

0 comments on commit a140e2d

Please sign in to comment.