Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added matchId check and timestamp conversion #2709

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 &&
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
!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;

Check warning on line 28 in src/v0/destinations/campaign_manager/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/campaign_manager/util.js#L28

Added line #L28 was not covered by tests
}

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);
});
});
Loading
Loading