-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
intentIqIdSystem.js
211 lines (194 loc) · 7.06 KB
/
intentIqIdSystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* This module adds IntentIqId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/intentIqIdSystem
* @requires module:modules/userId
*/
import { logError, logInfo } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js'
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_UID } from '../src/activities/modules.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').SubmoduleConfig} SubmoduleConfig
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
const PCID_EXPIRY = 365;
const MODULE_NAME = 'intentIqId';
export const FIRST_PARTY_KEY = '_iiq_fdata';
export var FIRST_PARTY_DATA_KEY = '_iiq_fdata';
export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME });
const INVALID_ID = 'INVALID_ID';
/**
* Generate standard UUID string
* @return {string}
*/
function generateGUID() {
let d = new Date().getTime();
const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return guid;
}
/**
* Read Intent IQ data from cookie or local storage
* @param key
* @return {string}
*/
export function readData(key) {
try {
if (storage.hasLocalStorage()) {
return storage.getDataFromLocalStorage(key);
}
if (storage.cookiesAreEnabled()) {
return storage.getCookie(key);
}
} catch (error) {
logError(error);
}
}
/**
* Store Intent IQ data in either cookie or local storage
* expiration date: 365 days
* @param key
* @param {string} value IntentIQ ID value to sintentIqIdSystem_spec.jstore
*/
function storeData(key, value, cookieStorageEnabled = false) {
try {
logInfo(MODULE_NAME + ': storing data: key=' + key + ' value=' + value);
if (value) {
if (storage.hasLocalStorage()) {
storage.setDataInLocalStorage(key, value);
}
const expiresStr = (new Date(Date.now() + (PCID_EXPIRY * (60 * 60 * 24 * 1000)))).toUTCString();
if (storage.cookiesAreEnabled() && cookieStorageEnabled) {
storage.setCookie(key, value, expiresStr, 'LAX');
}
}
} catch (error) {
logError(error);
}
}
/**
* Parse json if possible, else return null
* @param data
* @param {object|null}
*/
function tryParse(data) {
try {
return JSON.parse(data);
} catch (err) {
logError(err);
return null;
}
}
/** @type {Submodule} */
export const intentIqIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {{string}} value
* @returns {{intentIqId: {string}}|undefined}
*/
decode(value) {
return value && value != '' && INVALID_ID != value ? { 'intentIqId': value } : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @returns {IdResponse|undefined}
*/
getId(config) {
const configParams = (config && config.params) || {};
if (!configParams || typeof configParams.partner !== 'number') {
logError('User ID - intentIqId submodule requires a valid partner to be defined');
return;
}
const cookieStorageEnabled = typeof configParams.enableCookieStorage === 'boolean' ? configParams.enableCookieStorage : false
if (!FIRST_PARTY_DATA_KEY.includes(configParams.partner)) { FIRST_PARTY_DATA_KEY += '_' + configParams.partner; }
let rrttStrtTime = 0;
// Read Intent IQ 1st party id or generate it if none exists
let firstPartyData = tryParse(readData(FIRST_PARTY_KEY));
if (!firstPartyData || !firstPartyData.pcid || firstPartyData.pcidDate) {
const firstPartyId = generateGUID();
firstPartyData = { 'pcid': firstPartyId, 'pcidDate': Date.now() };
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), cookieStorageEnabled);
}
let partnerData = tryParse(readData(FIRST_PARTY_DATA_KEY));
if (!partnerData) partnerData = {};
// use protocol relative urls for http or https
let url = `https://api.intentiq.com/profiles_engine/ProfilesEngineServlet?at=39&mi=10&dpi=${configParams.partner}&pt=17&dpn=1`;
url += configParams.pcid ? '&pcid=' + encodeURIComponent(configParams.pcid) : '';
url += configParams.pai ? '&pai=' + encodeURIComponent(configParams.pai) : '';
url += firstPartyData.pcid ? '&iiqidtype=2&iiqpcid=' + encodeURIComponent(firstPartyData.pcid) : '';
url += firstPartyData.pid ? '&pid=' + encodeURIComponent(firstPartyData.pid) : '';
url += (partnerData.cttl) ? '&cttl=' + encodeURIComponent(partnerData.cttl) : '';
url += (partnerData.rrtt) ? '&rrtt=' + encodeURIComponent(partnerData.rrtt) : '';
url += firstPartyData.pcidDate ? '&iiqpciddate=' + encodeURIComponent(firstPartyData.pcidDate) : '';
const resp = function (callback) {
const callbacks = {
success: response => {
let respJson = tryParse(response);
// If response is a valid json and should save is true
if (respJson && respJson.ls) {
// Store pid field if found in response json
let shouldUpdateLs = false;
if ('pid' in respJson) {
firstPartyData.pid = respJson.pid;
shouldUpdateLs = true;
}
if ('cttl' in respJson) {
partnerData.cttl = respJson.cttl;
shouldUpdateLs = true;
}
// If should save and data is empty, means we should save as INVALID_ID
if (respJson.data == '') {
respJson.data = INVALID_ID;
} else {
partnerData.data = respJson.data;
shouldUpdateLs = true;
}
if (rrttStrtTime && rrttStrtTime > 0) {
partnerData.rrtt = Date.now() - rrttStrtTime;
shouldUpdateLs = true;
}
if (shouldUpdateLs === true) {
partnerData.date = Date.now();
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), cookieStorageEnabled);
storeData(FIRST_PARTY_DATA_KEY, JSON.stringify(partnerData), cookieStorageEnabled);
}
callback(respJson.data);
} else {
callback();
}
},
error: error => {
logError(MODULE_NAME + ': ID fetch encountered an error', error);
callback();
}
};
if (partnerData.date && partnerData.cttl && partnerData.data &&
Date.now() - partnerData.date < partnerData.cttl) { callback(partnerData.data); } else {
rrttStrtTime = Date.now();
ajax(url, callbacks, undefined, { method: 'GET', withCredentials: true });
}
};
return { callback: resp };
},
eids: {
'intentIqId': {
source: 'intentiq.com',
atype: 1
},
}
};
submodule('userId', intentIqIdSubmodule);