Skip to content

Commit

Permalink
[CSL-2690] Add items to trackRecommendationView (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
esezen committed Aug 14, 2023
1 parent cfd9297 commit 9103657
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions spec/src/modules/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4829,6 +4829,29 @@ describe('ConstructorIO - Tracker', () => {
)).to.equal(true);
});

it('Should respond with a valid response when required parameters and items are provided', (done) => {
const items = [{ item_id: '123', variation_id: '234' }, { item_id: 'abc' }];
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

tracker.on('success', (responseParams) => {
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
// Request
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('items').to.deep.equal(items);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
expect(responseParams).to.have.property('message');

done();
});

expect(tracker.trackRecommendationView({ items, ...requiredParameters }, userParameters)).to.equal(true);
});

it('Should respond with a valid response when parameters and user identifier are provided', (done) => {
const userId = 'bd2d9d1f097614c4b4de';
const { tracker } = new ConstructorIO({
Expand Down
7 changes: 7 additions & 0 deletions src/modules/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ class Tracker {
* @param {string} parameters.url - Current page URL
* @param {string} parameters.podId - Pod identifier
* @param {number} parameters.numResultsViewed - Number of results viewed
* @param {object[]} [parameters.items] - List of Product Item objects
* @param {number} [parameters.resultCount] - Total number of results
* @param {number} [parameters.resultPage] - Page number of results
* @param {string} [parameters.resultId] - Recommendation result identifier (returned in response from Constructor)
Expand All @@ -997,6 +998,7 @@ class Tracker {
* @example
* constructorio.tracker.trackRecommendationView(
* {
* items: [{ itemId: 'KMH876' }, { itemId: 'KMH140' }],
* resultCount: 22,
* resultPage: 2,
* resultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
Expand Down Expand Up @@ -1031,6 +1033,7 @@ class Tracker {
podId = pod_id,
num_results_viewed,
numResultsViewed = num_results_viewed,
items,
} = parameters;

if (!helpers.isNil(resultCount)) {
Expand Down Expand Up @@ -1063,6 +1066,10 @@ class Tracker {
bodyParams.num_results_viewed = numResultsViewed;
}

if (items && Array.isArray(items)) {
bodyParams.items = items.slice(0, 100).map((item) => helpers.toSnakeCaseKeys(item, false));
}

const requestUrl = `${requestPath}${applyParamsAsString({}, userParameters, this.options)}`;
const requestMethod = 'POST';
const requestBody = applyParams(bodyParams, userParameters, { ...this.options, requestMethod });
Expand Down
1 change: 1 addition & 0 deletions src/types/tracker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ declare class Tracker {
url: string;
podId: string;
numResultsViewed: number;
items?: ItemTracked[];
resultCount?: number;
resultPage?: number;
resultId?: string;
Expand Down

0 comments on commit 9103657

Please sign in to comment.