forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publinkIdSystem.js
166 lines (147 loc) · 4.67 KB
/
publinkIdSystem.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
/**
* This module adds the PublinkId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/publinkIdSystem
* @requires module:modules/userId
*/
import {submodule} from '../src/hook.js';
import {getStorageManager} from '../src/storageManager.js';
import {ajax} from '../src/ajax.js';
import { parseUrl, buildUrl, logError } from '../src/utils.js';
import {uspDataHandler} from '../src/adapterManager.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').ConsentData} ConsentData
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
const MODULE_NAME = 'publinkId';
const GVLID = 24;
const PUBLINK_COOKIE = '_publink';
const PUBLINK_S2S_COOKIE = '_publink_srv';
const PUBLINK_REQUEST_PATH = '/cvx/client/sync/publink';
const PUBLINK_REFRESH_PATH = '/cvx/client/sync/publink/refresh';
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});
function isHex(s) {
return /^[A-F0-9]+$/i.test(s);
}
function publinkIdUrl(params, consentData, storedId) {
let url = parseUrl('https://proc.ad.cpe.dotomi.com' + PUBLINK_REFRESH_PATH);
url.search = {
mpn: 'Prebid.js',
mpv: '$prebid.version$',
};
if (consentData) {
url.search.gdpr = (consentData.gdprApplies) ? 1 : 0;
url.search.gdpr_consent = consentData.consentString;
}
if (params) {
if (params.e) {
// if there's an email parameter call the request path
url.search.deh = params.e;
url.pathname = PUBLINK_REQUEST_PATH;
}
if (params.site_id) { url.search.sid = params.site_id; }
if (params.api_key) { url.search.apikey = params.api_key; }
}
if (storedId) {
url.search.publink = storedId;
}
const usPrivacyString = uspDataHandler.getConsentData();
if (usPrivacyString && typeof usPrivacyString === 'string') {
url.search.us_privacy = usPrivacyString;
}
return buildUrl(url);
}
function makeCallback(config = {}, consentData, storedId) {
return function(prebidCallback) {
const options = {method: 'GET', withCredentials: true};
let handleResponse = function(responseText, xhr) {
if (xhr.status === 200) {
let response = JSON.parse(responseText);
if (response) {
prebidCallback(response.publink);
}
}
};
if ((config.params && config.params.e && isHex(config.params.e)) || storedId) {
ajax(publinkIdUrl(config.params, consentData, storedId), handleResponse, undefined, options);
} else if (config.params.e) {
logError('params.e must be a hex string');
}
}
}
function getlocalValue() {
let result;
function getData(key) {
let value;
if (storage.hasLocalStorage()) {
value = storage.getDataFromLocalStorage(key);
}
if (!value) {
value = storage.getCookie(key);
}
if (typeof value === 'string') {
// if it's a json object parse it and return the publink value, otherwise assume the value is the id
if (value.charAt(0) === '{') {
try {
const obj = JSON.parse(value);
if (obj) {
return obj.publink;
}
} catch (e) {
logError(e);
}
} else {
return value;
}
}
}
result = getData(PUBLINK_S2S_COOKIE);
if (!result) {
result = getData(PUBLINK_COOKIE);
}
return result;
}
/** @type {Submodule} */
export const publinkIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
gvlid: GVLID,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {string} publinkId encrypted userid
* @returns {{publinkId: string} | undefined}
*/
decode(publinkId) {
return {publinkId: publinkId};
},
/**
* performs action to obtain id
* Use a publink cookie first if it is present, otherwise use prebids copy, if neither are available callout to get a new id
* @function
* @param {SubmoduleConfig} [config] Config object with params and storage properties
* @param {ConsentData|undefined} consentData GDPR consent
* @param {(Object|undefined)} storedId Previously cached id
* @returns {IdResponse}
*/
getId: function(config, consentData, storedId) {
const localValue = getlocalValue();
if (localValue) {
return {id: localValue};
}
return {callback: makeCallback(config, consentData, storedId)};
},
eids: {
'publinkId': {
source: 'epsilon.com',
atype: 3
},
},
};
submodule('userId', publinkIdSubmodule);