-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mwOpenLinkIdSystem.js
154 lines (133 loc) · 4 KB
/
mwOpenLinkIdSystem.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
/**
* This module adds MediaWallah OpenLink to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/mwOpenLinkIdSystem
* @requires module:modules/userId
*/
import { timestamp, logError, deepClone, generateUUID, isPlainObject } 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').SubmoduleParams} SubmoduleParams
*/
const openLinkID = {
name: 'mwol',
cookie_expiration: (86400 * 1000 * 365 * 1) // 1 year
}
const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: openLinkID.name});
function getExpirationDate() {
return (new Date(timestamp() + openLinkID.cookie_expiration)).toGMTString();
}
function isValidConfig(configParams) {
if (!configParams) {
logError('User ID - mwOlId submodule requires configParams');
return false;
}
if (!configParams.accountId) {
logError('User ID - mwOlId submodule requires accountId to be defined');
return false;
}
if (!configParams.partnerId) {
logError('User ID - mwOlId submodule requires partnerId to be defined');
return false;
}
return true;
}
function deserializeMwOlId(mwOlIdStr) {
const mwOlId = {};
const mwOlIdArr = mwOlIdStr.split(',');
mwOlIdArr.forEach(function(value) {
const pair = value.split(':');
// unpack a value of 1 as true
mwOlId[pair[0]] = +pair[1] === 1 ? true : pair[1];
});
return mwOlId;
}
function serializeMwOlId(mwOlId) {
let components = [];
if (mwOlId.eid) {
components.push('eid:' + mwOlId.eid);
}
if (mwOlId.ibaOptout) {
components.push('ibaOptout:1');
}
if (mwOlId.ccpaOptout) {
components.push('ccpaOptout:1');
}
return components.join(',');
}
function readCookie(name) {
if (!name) name = openLinkID.name;
const mwOlIdStr = storage.getCookie(name);
if (mwOlIdStr) {
return deserializeMwOlId(decodeURIComponent(mwOlIdStr));
}
return null;
}
function writeCookie(mwOlId) {
if (mwOlId) {
const mwOlIdStr = encodeURIComponent(serializeMwOlId(mwOlId));
storage.setCookie(openLinkID.name, mwOlIdStr, getExpirationDate(), 'lax');
}
}
function register(configParams, olid) {
const { accountId, partnerId, uid } = configParams;
const url = 'https://ol.mediawallahscript.com/?account_id=' + accountId +
'&partner_id=' + partnerId +
'&uid=' + uid +
'&olid=' + olid +
'&cb=' + Math.random()
;
ajax(url);
}
function setID(configParams) {
if (!isValidConfig(configParams)) return undefined;
const mwOlId = readCookie();
const newMwOlId = mwOlId ? deepClone(mwOlId) : {eid: generateUUID()};
writeCookie(newMwOlId);
register(configParams, newMwOlId.eid);
return {
id: newMwOlId
};
};
/* End MW */
export { writeCookie };
/** @type {Submodule} */
export const mwOpenLinkIdSubModule = {
/**
* used to link submodule with config
* @type {string}
*/
name: 'mwOpenLinkId',
/**
* decode the stored id value for passing to bid requests
* @function
* @param {MwOlId} mwOlId
* @return {(Object|undefined}
*/
decode(mwOlId) {
const id = mwOlId && isPlainObject(mwOlId) ? mwOlId.eid : undefined;
return id ? { 'mwOpenLinkId': id } : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [submoduleParams]
* @returns {id:MwOlId | undefined}
*/
getId(submoduleConfig) {
const submoduleConfigParams = (submoduleConfig && submoduleConfig.params) || {};
if (!isValidConfig(submoduleConfigParams)) return undefined;
return setID(submoduleConfigParams);
},
eids: {
'mwOpenLinkId': {
source: 'mediawallahscript.com',
atype: 1
},
}
};
submodule('userId', mwOpenLinkIdSubModule);