Skip to content

Commit

Permalink
fix: added matchId check and timestamp conversion (#2709)
Browse files Browse the repository at this point in the history
* fix: added matchId check and timestamp conversion

* fix: added matchId check and timestamp conversion

* fix: timestamp logic to function

* fix: lint fixes

* fix: move to utility

* fix: move to utility
  • Loading branch information
aashishmalik committed Oct 11, 2023
1 parent d681d5e commit f49244d
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
"destKey": "timestampMicros",
"sourceKeys": "timestamp",
"sourceFromGenericMap": true,
"required": true,
"metadata": {
"type": "microSecondTimestamp"
}
"required": true
},
{
"destKey": "floodlightActivityId",
Expand Down
43 changes: 14 additions & 29 deletions src/v0/destinations/campaign_manager/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const {
EncryptionSource,
} = require('./config');

const { convertToMicroseconds } = require('./util');

const { InstrumentationError } = require('../../util/errorTypes');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand Down Expand Up @@ -72,7 +74,8 @@ function processTrack(message, metadata, destination) {
delete requestJson.childDirectedTreatment;
delete requestJson.limitAdTracking;
}
requestJson.timestampMicros = requestJson.timestampMicros.toString();

requestJson.timestampMicros = convertToMicroseconds(requestJson.timestampMicros).toString();

const encryptionInfo = {};
// prepare encrptionInfo if encryptedUserId or encryptedUserIdCandidates is given
Expand Down Expand Up @@ -138,35 +141,17 @@ function postValidateRequest(response) {
);
}

let count = 0;

if (response.body.JSON.conversions[0].gclid) {
count += 1;
}

if (response.body.JSON.conversions[0].dclid) {
count += 1;
}

if (response.body.JSON.conversions[0].encryptedUserId) {
count += 1;
}

if (response.body.JSON.conversions[0].encryptedUserIdCandidates) {
count += 1;
}

if (response.body.JSON.conversions[0].mobileDeviceId) {
count += 1;
}

if (response.body.JSON.conversions[0].impressionId) {
count += 1;
}

if (count !== 1) {
if (
!response.body.JSON.conversions[0].gclid &&
!response.body.JSON.conversions[0].matchId &&
!response.body.JSON.conversions[0].dclid &&
!response.body.JSON.conversions[0].encryptedUserId &&
!response.body.JSON.conversions[0].encryptedUserIdCandidates &&
!response.body.JSON.conversions[0].mobileDeviceId &&
!response.body.JSON.conversions[0].impressionId
) {
throw new InstrumentationError(
'[CAMPAIGN MANAGER (DCM)]: For CM360 we need one of encryptedUserId,encryptedUserIdCandidates, matchId, mobileDeviceId, gclid, dclid, impressionId.',
'[CAMPAIGN MANAGER (DCM)]: Atleast one of encryptedUserId,encryptedUserIdCandidates, matchId, mobileDeviceId, gclid, dclid, impressionId.',
);
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/v0/destinations/campaign_manager/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function convertToMicroseconds(input) {
const timestamp = Date.parse(input);

if (!Number.isNaN(timestamp)) {
// If the input is a valid date string, timestamp will be a number
if (input.includes('Z')) {
// ISO 8601 date string with milliseconds
return timestamp * 1000;
}
// to handle case of "2022-11-17T00:22:02.903+05:30" strings
return timestamp.toString().length === 13 ? timestamp * 1000 : timestamp * 1000000;
}

if (/^\d+$/.test(input)) {
// If the input is a numeric string (assume microseconds or milliseconds)
if (input.length === 13) {
// equal to 13 indicates milliseconds
return parseInt(input, 10) * 1000;
}

if (input.length === 10) {
// equal to 10 indicates seconds
return parseInt(input, 10) * 1000000;
}
// Otherwise, assume microseconds
return parseInt(input, 10);
}
return timestamp;
}

module.exports = {
convertToMicroseconds,
};
23 changes: 23 additions & 0 deletions src/v0/destinations/campaign_manager/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { convertToMicroseconds } = require('./util');

describe('convertToMicroseconds utility test', () => {
it('ISO 8601 input', () => {
expect(convertToMicroseconds('2021-01-04T08:25:04.780Z')).toEqual(1609748704780000);
});

it('unix microseconds input', () => {
expect(convertToMicroseconds('1668624722903333')).toEqual(1668624722903333);
});

it('non numeric time input', () => {
expect(convertToMicroseconds('2022-11-17T00:22:02.903+05:30')).toEqual(1668624722903000);
});

it('unix seconds input', () => {
expect(convertToMicroseconds('1697013935')).toEqual(1697013935000000);
});

it('unix miliseconds input', () => {
expect(convertToMicroseconds('1697013935000')).toEqual(1697013935000000);
});
});

0 comments on commit f49244d

Please sign in to comment.