diff --git a/spec/src/modules/tracker.js b/spec/src/modules/tracker.js index b53c8de4..db005f3d 100644 --- a/spec/src/modules/tracker.js +++ b/spec/src/modules/tracker.js @@ -419,6 +419,33 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + + // Response + expect(responseParams).to.have.property('method').to.equal('GET'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackSessionStart({ + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -893,6 +920,33 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + + // Response + expect(responseParams).to.have.property('method').to.equal('GET'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackInputFocus({ + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -1388,6 +1442,33 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + + // Response + expect(responseParams).to.have.property('method').to.equal('GET'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAutocompleteSelect(term, requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -2013,6 +2094,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackItemDetailLoad(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -2332,6 +2442,33 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + + // Response + expect(responseParams).to.have.property('method').to.equal('GET'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackSearchSubmit(term, requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -2822,6 +2959,33 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + + // Response + expect(responseParams).to.have.property('method').to.equal('GET'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackSearchResultsLoaded(term, requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -3416,6 +3580,33 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + + // Response + expect(responseParams).to.have.property('method').to.equal('GET'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackSearchResultClick(term, requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -4090,6 +4281,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackConversion(term, requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -4793,6 +5013,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackPurchase(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -5352,6 +5601,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackRecommendationView(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -5861,6 +6139,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackRecommendationClick(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -6388,6 +6695,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackBrowseResultsLoaded(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -6934,6 +7270,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackBrowseResultClick(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ @@ -7458,6 +7823,35 @@ describe('ConstructorIO - Tracker', () => { })).to.equal(true); }); + it('Should respond with a valid response with dateTime', (done) => { + const dateTime = 123456789; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + tracker.on('success', (responseParams) => { + const requestedParams = helpers.extractUrlParamsFromFetch(fetchSpy); + const requestedBody = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestedParams).to.have.property('_dt').to.equal(dateTime.toString()); + expect(requestedBody).to.have.property('_dt').to.equal(dateTime); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackGenericResultClick(requiredParameters, { + ...userParameters, + dateTime, + })).to.equal(true); + }); + it('Should respond with a valid response with referer', (done) => { const referer = 'https://localhost'; const { tracker } = new ConstructorIO({ diff --git a/src/modules/tracker.js b/src/modules/tracker.js index ceeee08c..705be85f 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -16,6 +16,7 @@ function applyParams(parameters, userParameters, options) { segments, testCells, originReferrer, + dateTime, } = userParameters || {}; let aggregateParams = Object.assign(parameters); @@ -63,7 +64,7 @@ function applyParams(parameters, userParameters, options) { aggregateParams.origin_referrer = originReferrer; } - aggregateParams._dt = Date.now(); + aggregateParams._dt = dateTime || Date.now(); aggregateParams.beacon = true; aggregateParams = helpers.cleanParams(aggregateParams); @@ -202,6 +203,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -242,6 +244,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -290,6 +293,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -390,6 +394,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -490,6 +495,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -576,6 +582,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -674,6 +681,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -789,6 +797,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -923,6 +932,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -1025,6 +1035,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -1156,6 +1167,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -1309,6 +1321,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -1461,6 +1474,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} @@ -1608,6 +1622,7 @@ class Tracker { * @param {string} [userParameters.userIp] - Client user IP * @param {string} [userParameters.userAgent] - Client user agent * @param {string} [userParameters.acceptLanguage] - Client accept language + * @param {string} [userParameters.dateTime] - Time since epoch in milliseconds * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} diff --git a/src/types/tracker.d.ts b/src/types/tracker.d.ts index 500be7e2..1b9e1d6d 100644 --- a/src/types/tracker.d.ts +++ b/src/types/tracker.d.ts @@ -14,6 +14,7 @@ export interface TrackerUserParameters { userIp?: string; userAgent?: string; acceptLanguage?: string; + dateTime?: number; } declare class Tracker {