forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubmaticIdSystem.js
156 lines (133 loc) · 4.59 KB
/
pubmaticIdSystem.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
import { logInfo, logError, isNumber, isStr, isEmptyStr } 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';
import { uspDataHandler, coppaDataHandler, gppDataHandler } from '../src/adapterManager.js';
const MODULE_NAME = 'pubmaticId';
const GVLID = 76;
export const STORAGE_NAME = 'pubmaticId';
const STORAGE_EXPIRES = 30; // days
const STORAGE_REFRESH_IN_SECONDS = 24 * 3600; // 24 Hours
const LOG_PREFIX = 'PubMatic User ID: ';
const VERSION = '1';
const API_URL = 'https://image6.pubmatic.com/AdServer/UCookieSetPug?oid=5&p=';
export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME });
function generateQueryStringParams(config, consentData) {
const uspString = uspDataHandler.getConsentData();
const coppaValue = coppaDataHandler.getCoppa();
const gppConsent = gppDataHandler.getConsentData();
const params = {
publisherId: config.params.publisherId,
gdpr: (consentData && consentData?.gdprApplies) ? 1 : 0,
gdpr_consent: consentData && consentData?.consentString ? encodeURIComponent(consentData.consentString) : '',
src: 'pbjs_uid',
ver: VERSION,
coppa: Number(coppaValue),
us_privacy: uspString ? encodeURIComponent(uspString) : '',
gpp: gppConsent?.gppString ? encodeURIComponent(gppConsent.gppString) : '',
gpp_sid: gppConsent?.applicableSections?.length ? encodeURIComponent(gppConsent.applicableSections.join(',')) : ''
};
return params;
}
function buildUrl(config, consentData) {
let baseUrl = `${API_URL}${config.params.publisherId}`;
const params = generateQueryStringParams(config, consentData);
Object.keys(params).forEach((key) => {
baseUrl += `&${key}=${params[key]}`;
});
return baseUrl;
}
function deleteFromAllStorages(key) {
const cKeys = [key, `${key}_cst`, `${key}_last`, `${key}_exp`];
cKeys.forEach((cKey) => {
if (storage.getCookie(cKey)) {
storage.setCookie(cKey, '', new Date(0).toUTCString());
}
});
const lsKeys = [key, `${key}_cst`, `${key}_last`, `${key}_exp`];
lsKeys.forEach((lsKey) => {
if (storage.getDataFromLocalStorage(lsKey)) {
storage.removeDataFromLocalStorage(lsKey);
}
});
}
function getSuccessAndErrorHandler(callback) {
return {
success: (response) => {
let responseObj;
try {
responseObj = JSON.parse(response);
logInfo(LOG_PREFIX + 'response received from the server', responseObj);
} catch (error) {}
if (responseObj && isStr(responseObj.id) && !isEmptyStr(responseObj.id)) {
callback(responseObj);
} else {
deleteFromAllStorages(STORAGE_NAME);
callback();
}
},
error: (error) => {
deleteFromAllStorages(STORAGE_NAME);
logError(LOG_PREFIX + 'getId fetch encountered an error', error);
callback();
}
};
}
function hasRequiredConfig(config) {
if (!config || !config.storage || !config.params) {
logError(LOG_PREFIX + 'config.storage and config.params should be passed.');
return false;
}
if (!isNumber(config.params.publisherId)) {
logError(LOG_PREFIX + 'config.params.publisherId (int) should be provided.');
return false;
}
if (config.storage.name !== STORAGE_NAME) {
logError(LOG_PREFIX + `config.storage.name should be '${STORAGE_NAME}'.`);
return false;
}
if (config.storage.expires !== STORAGE_EXPIRES) {
logError(LOG_PREFIX + `config.storage.expires should be ${STORAGE_EXPIRES}.`);
return false;
}
if (config.storage.refreshInSeconds !== STORAGE_REFRESH_IN_SECONDS) {
logError(LOG_PREFIX + `config.storage.refreshInSeconds should be ${STORAGE_REFRESH_IN_SECONDS}.`);
return false;
}
return true;
}
export const pubmaticIdSubmodule = {
name: MODULE_NAME,
gvlid: GVLID,
decode(value) {
if (isStr(value.id) && !isEmptyStr(value.id)) {
return { pubmaticId: value.id };
}
return undefined;
},
getId(config, consentData) {
if (!hasRequiredConfig(config)) {
return undefined;
}
const resp = (callback) => {
logInfo(LOG_PREFIX + 'requesting an ID from the server');
const url = buildUrl(config, consentData);
ajax(url, getSuccessAndErrorHandler(callback), null, {
method: 'GET',
withCredentials: true,
});
};
return { callback: resp };
},
eids: {
'pubmaticId': {
source: 'esp.pubmatic.com',
atype: 1,
getValue: (data) => {
return data;
}
},
}
};
submodule('userId', pubmaticIdSubmodule);