forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedIdSystem.js
193 lines (176 loc) · 6.05 KB
/
sharedIdSystem.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
/**
* This module adds SharedId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/sharedIdSystem
* @requires module:modules/userId
*/
import {parseUrl, buildUrl, triggerPixel, logInfo, hasDeviceAccess, generateUUID} from '../src/utils.js';
import {submodule} from '../src/hook.js';
import {coppaDataHandler} from '../src/adapterManager.js';
import {getStorageManager} from '../src/storageManager.js';
import {VENDORLESS_GVLID} from '../src/consentHandler.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
import {domainOverrideToRootDomain} from '../libraries/domainOverrideToRootDomain/index.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').SubmoduleConfig} SubmoduleConfig
* @typedef {import('../modules/userId/index.js').SubmoduleParams} SubmoduleParams
* @typedef {import('../modules/userId/index.js').ConsentData} ConsentData
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: 'sharedId'});
const COOKIE = 'cookie';
const LOCAL_STORAGE = 'html5';
const OPTOUT_NAME = '_pubcid_optout';
const PUB_COMMON_ID = 'PublisherCommonId';
/**
* Read a value either from cookie or local storage
* @param {string} name Name of the item
* @param {string} type storage type override
* @returns {string|null} a string if item exists
*/
function readValue(name, type) {
if (type === COOKIE) {
return storage.getCookie(name);
} else if (type === LOCAL_STORAGE) {
if (storage.hasLocalStorage()) {
const expValue = storage.getDataFromLocalStorage(`${name}_exp`);
if (!expValue) {
return storage.getDataFromLocalStorage(name);
} else if ((new Date(expValue)).getTime() - Date.now() > 0) {
return storage.getDataFromLocalStorage(name)
}
}
}
}
function getIdCallback(pubcid, pixelUrl) {
return function (callback, getStoredId) {
if (pixelUrl) {
queuePixelCallback(pixelUrl, pubcid, () => {
callback(getStoredId() || pubcid);
})();
} else {
callback(pubcid);
}
}
}
function queuePixelCallback(pixelUrl, id = '', callback) {
if (!pixelUrl) {
return;
}
// Use pubcid as a cache buster
const urlInfo = parseUrl(pixelUrl);
urlInfo.search.id = encodeURIComponent('pubcid:' + id);
const targetUrl = buildUrl(urlInfo);
return function () {
triggerPixel(targetUrl, callback);
};
}
function hasOptedOut() {
return !!((storage.cookiesAreEnabled() && readValue(OPTOUT_NAME, COOKIE)) ||
(storage.hasLocalStorage() && readValue(OPTOUT_NAME, LOCAL_STORAGE)));
}
export const sharedIdSystemSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: 'sharedId',
aliasName: 'pubCommonId',
gvlid: VENDORLESS_GVLID,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {string} value
* @param {SubmoduleConfig} config
* @returns {{pubcid:string}}
*/
decode(value, config) {
if (hasOptedOut()) {
logInfo('PubCommonId decode: Has opted-out');
return undefined;
}
logInfo(' Decoded value PubCommonId ' + value);
const idObj = {'pubcid': value};
return idObj;
},
/**
* performs action to obtain id
* @function
* @param {SubmoduleConfig} [config] Config object with params and storage properties
* @param {Object} consentData
* @param {string} storedId Existing pubcommon id
* @returns {IdResponse}
*/
getId: function (config = {}, consentData, storedId) {
if (hasOptedOut()) {
logInfo('PubCommonId: Has opted-out');
return;
}
const coppa = coppaDataHandler.getCoppa();
if (coppa) {
logInfo('PubCommonId: IDs not provided for coppa requests, exiting PubCommonId');
return;
}
const {params: {create = true, pixelUrl} = {}} = config;
let newId = storedId;
if (!newId) {
try {
if (typeof window[PUB_COMMON_ID] === 'object') {
// If the page includes its own pubcid module, then save a copy of id.
newId = window[PUB_COMMON_ID].getId();
}
} catch (e) {
}
if (!newId) newId = (create && hasDeviceAccess()) ? generateUUID() : undefined;
}
return {id: newId, callback: getIdCallback(newId, pixelUrl)};
},
/**
* performs action to extend an id. There are generally two ways to extend the expiration time
* of stored id: using pixelUrl or return the id and let main user id module write it again with
* the new expiration time.
*
* PixelUrl, if defined, should point back to a first party domain endpoint. On the server
* side, there is either a plugin, or customized logic to read and write back the pubcid cookie.
* The extendId function itself should return only the callback, and not the id itself to avoid
* having the script-side overwriting server-side. This applies to both pubcid and sharedid.
*
* On the other hand, if there is no pixelUrl, then the extendId should return storedId so that
* its expiration time is updated.
*
* @function
* @param {SubmoduleParams} [config]
* @param {ConsentData|undefined} consentData
* @param {Object} storedId existing id
* @returns {IdResponse|undefined}
*/
extendId: function(config = {}, consentData, storedId) {
if (hasOptedOut()) {
logInfo('PubCommonId: Has opted-out');
return {id: undefined};
}
const coppa = coppaDataHandler.getCoppa();
if (coppa) {
logInfo('PubCommonId: IDs not provided for coppa requests, exiting PubCommonId');
return;
}
const {params: {extend = false, pixelUrl} = {}} = config;
if (extend) {
if (pixelUrl) {
const callback = queuePixelCallback(pixelUrl, storedId);
return {callback: callback};
} else {
return {id: storedId};
}
}
},
domainOverride: domainOverrideToRootDomain(storage, 'sharedId'),
eids: {
'pubcid': {
source: 'pubcid.org',
atype: 1
},
}
};
submodule('userId', sharedIdSystemSubmodule);