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

ATS-analytics - add retry logic to not fire request for envelope every time, and cut down analytics requests to 1/10 #5839

Merged
merged 2 commits into from
Oct 14, 2020
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
22 changes: 15 additions & 7 deletions modules/atsAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ let atsAnalyticsAdapter = Object.assign(adapter(
callHandler(eventType, args);
}
if (eventType === CONSTANTS.EVENTS.AUCTION_END) {
// send data to ats analytic endpoint
try {
let dataToSend = {'Data': atsAnalyticsAdapter.context.events};
let strJSON = JSON.stringify(dataToSend);
ajax(atsAnalyticsAdapter.context.host, function () {
}, strJSON, {method: 'POST', contentType: 'application/json'});
} catch (err) {
if (atsAnalyticsAdapter.shouldFireRequest()) {
// send data to ats analytic endpoint
try {
let dataToSend = {'Data': atsAnalyticsAdapter.context.events};
let strJSON = JSON.stringify(dataToSend);
utils.logInfo('atsAnalytics tried to send analytics data!');
ajax(atsAnalyticsAdapter.context.host, function () {
}, strJSON, {method: 'POST', contentType: 'application/json'});
} catch (err) {
}
}
}
}
Expand All @@ -141,6 +144,11 @@ let atsAnalyticsAdapter = Object.assign(adapter(
// save the base class function
atsAnalyticsAdapter.originEnableAnalytics = atsAnalyticsAdapter.enableAnalytics;

// add check to not fire request every time, but instead to send 1/10 events
atsAnalyticsAdapter.shouldFireRequest = function () {
return (Math.floor((Math.random() * 11)) === 10);
}

// override enableAnalytics so we can get access to the config passed in from the page
atsAnalyticsAdapter.enableAnalytics = function (config) {
if (!config.options.pid) {
Expand Down
17 changes: 15 additions & 2 deletions modules/identityLinkIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import * as utils from '../src/utils.js'
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import {getStorageManager} from '../src/storageManager.js';

export const storage = getStorageManager();

/** @type {Submodule} */
export const identityLinkSubmodule = {
Expand Down Expand Up @@ -75,7 +78,6 @@ export const identityLinkSubmodule = {
};
// return envelope from third party endpoint
function getEnvelope(url, callback) {
utils.logInfo('A 3P retrieval is attempted!');
const callbacks = {
success: response => {
let responseObj;
Expand All @@ -93,7 +95,18 @@ function getEnvelope(url, callback) {
callback();
}
};
ajax(url, callbacks, undefined, { method: 'GET', withCredentials: true });

if (!storage.getCookie('_lr_retry_request')) {
setRetryCookie();
utils.logInfo('A 3P retrieval is attempted!');
ajax(url, callbacks, undefined, { method: 'GET', withCredentials: true });
}
}

function setRetryCookie() {
let now = new Date();
now.setTime(now.getTime() + 3600000);
storage.setCookie('_lr_retry_request', 'true', now.toUTCString());
}

submodule('userId', identityLinkSubmodule);
3 changes: 1 addition & 2 deletions test/spec/modules/atsAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('ats analytics adapter', function () {

describe('track', function () {
it('builds and sends request and response data', function () {
sinon.spy(atsAnalyticsAdapter, 'track');
sinon.stub(atsAnalyticsAdapter, 'shouldFireRequest').returns(true);

let initOptions = {
pid: '10433394',
Expand Down Expand Up @@ -134,7 +134,6 @@ describe('ats analytics adapter', function () {
let requests = server.requests.filter(req => {
return req.url.indexOf(initOptions.host) > -1;
});

expect(requests.length).to.equal(1);

let realAfterBid = JSON.parse(requests[0].requestBody);
Expand Down
30 changes: 30 additions & 0 deletions test/spec/modules/identityLinkIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {identityLinkSubmodule} from 'modules/identityLinkIdSystem.js';
import * as utils from 'src/utils.js';
import {server} from 'test/mocks/xhr.js';
import {getStorageManager} from '../../../src/storageManager.js';

export const storage = getStorageManager();

const pid = '14';
const defaultConfigParams = {pid: pid};
Expand All @@ -11,6 +14,8 @@ describe('IdentityLinkId tests', function () {

beforeEach(function () {
logErrorStub = sinon.stub(utils, 'logError');
// remove _lr_retry_request cookie before test
storage.setCookie('_lr_retry_request', 'true', 'Thu, 01 Jan 1970 00:00:01 GMT');
});

afterEach(function () {
Expand Down Expand Up @@ -124,4 +129,29 @@ describe('IdentityLinkId tests', function () {
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should not call the LiveRamp envelope endpoint if cookie _lr_retry_request exist', function () {
let now = new Date();
now.setTime(now.getTime() + 3000);
storage.setCookie('_lr_retry_request', 'true', now.toUTCString());
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request).to.be.eq(undefined);
});

it('should call the LiveRamp envelope endpoint if cookie _lr_retry_request does not exist', function () {
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request.url).to.be.eq('https://api.rlcdn.com/api/identity/envelope?pid=14');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});
});