forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtncIdSystem.js
154 lines (138 loc) · 4.77 KB
/
tncIdSystem.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 TncId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/tncIdSystem
* @requires module:modules/userId
*/
import { submodule } from '../src/hook.js';
import { parseUrl, buildUrl, logInfo, logMessage, logError } from '../src/utils.js';
import { getStorageManager } from '../src/storageManager.js';
import { loadExternalScript } from '../src/adloader.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 = 'tncId';
const TNC_API_URL = 'https://js.tncid.app/remote.js';
const TNC_DEFAULT_NS = '__tnc';
const TNC_PREBID_NS = '__tncPbjs';
const TNC_PREBIDJS_PROVIDER_ID = 'c8549079-f149-4529-a34b-3fa91ef257d1';
const TNC_LOCAL_VALUE_KEY = 'tncid';
let moduleConfig = null;
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});
function fixURL(config, ns) {
config.params = (config && config.params) ? config.params : {};
config.params.url = config.params.url || TNC_API_URL;
let url = parseUrl(config.params.url);
url.search = url.search || {};
let providerId = config.params.publisherId || config.params.providerId || url.search.publisherId || url.search.providerId || TNC_PREBIDJS_PROVIDER_ID;
delete url.search.publisherId;
url.search.providerId = providerId;
url.search.ns = ns;
return url;
}
const loadRemoteScript = function(url) {
return new Promise((resolve, reject) => {
let endpoint = buildUrl(url);
logMessage('TNC Endpoint', endpoint);
loadExternalScript(endpoint, MODULE_TYPE_UID, MODULE_NAME, resolve);
});
}
function TNCObject(ns) {
let tnc = window[ns];
tnc = typeof tnc !== 'undefined' && tnc !== null && typeof tnc.ready == 'function' ? tnc : {
ready: function(f) { this.ready.q = this.ready.q || []; return typeof f == 'function' ? (this.ready.q.push(f), this) : new Promise(resolve => this.ready.q.push(resolve)); },
};
window[ns] = tnc;
return tnc;
}
function getlocalValue(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 tncid value, otherwise assume the value is the id
if (value.charAt(0) === '{') {
try {
const obj = JSON.parse(value);
if (obj) {
return obj.tncid;
}
} catch (e) {
logError(e);
}
} else {
return value;
}
}
return null;
}
const tncCallback = async function(cb) {
try {
let tncNS = TNC_DEFAULT_NS;
let tncid = getlocalValue(TNC_LOCAL_VALUE_KEY);
if (!window[tncNS] || typeof window[tncNS].ready !== 'function') {
tncNS = TNC_PREBID_NS; // Register a new namespace for TNC global object
let url = fixURL(moduleConfig, tncNS);
if (!url) return cb();
TNCObject(tncNS); // create minimal TNC object
await loadRemoteScript(url); // load remote script
}
if (!tncid) {
await new Promise(resolve => window[tncNS].ready(resolve));
tncid = await window[tncNS].getTNCID('prebid'); // working directly with (possibly) overridden TNC Object
logMessage('tncId Module - tncid retrieved from remote script', tncid);
} else {
logMessage('tncId Module - tncid already exists', tncid);
window[tncNS].ready(() => window[tncNS].getTNCID('prebid'));
}
return cb(tncid);
} catch (err) {
logMessage('tncId Module', err);
return cb();
}
}
export const tncidSubModule = {
name: MODULE_NAME,
decode(id) {
return {
tncid: id
};
},
gvlid: 750,
/**
* performs action to obtain id
* Use a tncid cookie first if it is present, otherwise callout to get a new id
* @function
* @param {SubmoduleConfig} [config] Config object with params and storage properties
* @param {ConsentData} [consentData] GDPR consent
* @returns {IdResponse}
*/
getId(config, consentData) {
const gdpr = (consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies) ? 1 : 0;
const consentString = gdpr ? consentData.consentString : '';
if (gdpr && !consentString) {
logInfo('Consent string is required for TNCID module');
return;
}
moduleConfig = config;
return {
callback: function (cb) { return tncCallback(cb); }
// callback: tncCallback
}
},
eids: {
'tncid': {
source: 'thenewco.it',
atype: 3
},
}
}
submodule('userId', tncidSubModule)