forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabrickIdSystem.js
196 lines (182 loc) · 5.84 KB
/
fabrickIdSystem.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
194
195
196
/**
* This module adds neustar's fabrickId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/fabrickIdSystem
* @requires module:modules/userId
*/
import { logError } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import { getRefererInfo } from '../src/refererDetection.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
*/
/** @type {Submodule} */
export const fabrickIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: 'fabrickId',
/**
* decode the stored id value for passing to bid requests
* @function decode
* @param {(Object|string)} value
* @returns {(Object|undefined)}
*/
decode(value) {
if (value && value.fabrickId) {
return { 'fabrickId': value.fabrickId };
} else {
return undefined;
}
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function getId
* @param {SubmoduleConfig} [config]
* @param {ConsentData}
* @param {Object} cacheIdObj - existing id, if any consentData]
* @returns {IdResponse|undefined}
*/
getId(config, consentData, cacheIdObj) {
try {
const configParams = (config && config.params) || {};
if (window.fabrickMod1) {
window.fabrickMod1(configParams, consentData, cacheIdObj);
}
if (!configParams || !configParams.apiKey || typeof configParams.apiKey !== 'string') {
logError('fabrick submodule requires an apiKey.');
return;
}
try {
let url = _getBaseUrl(configParams);
let keysArr = Object.keys(configParams);
for (let i in keysArr) {
let k = keysArr[i];
if (k === 'url' || k === 'refererInfo' || (k.length > 3 && k.substring(0, 3) === 'max')) {
continue;
}
let v = configParams[k];
if (Array.isArray(v)) {
for (let j in v) {
if (typeof v[j] === 'string' || typeof v[j] === 'number') {
url += `${k}=${v[j]}&`;
}
}
} else if (typeof v === 'string' || typeof v === 'number') {
url += `${k}=${v}&`;
}
}
// pull off the trailing &
url = url.slice(0, -1);
const referer = _getRefererInfo(configParams);
const refs = new Map();
_setReferrer(refs, referer.topmostLocation);
if (referer.stack && referer.stack[0]) {
_setReferrer(refs, referer.stack[0]);
}
_setReferrer(refs, referer.canonicalUrl);
_setReferrer(refs, window.location.href);
refs.forEach(v => {
url = appendUrl(url, 'r', v, configParams);
});
const resp = function (callback) {
const callbacks = {
success: response => {
if (window.fabrickMod2) {
return window.fabrickMod2(
callback, response, configParams, consentData, cacheIdObj);
} else {
let responseObj;
if (response) {
try {
responseObj = JSON.parse(response);
} catch (error) {
logError(error);
responseObj = {};
}
}
callback(responseObj);
}
},
error: error => {
logError(`fabrickId fetch encountered an error`, error);
callback();
}
};
ajax(url, callbacks, null, {method: 'GET', withCredentials: true});
};
return {callback: resp};
} catch (e) {
logError(`fabrickIdSystem encountered an error`, e);
}
} catch (e) {
logError(`fabrickIdSystem encountered an error`, e);
}
},
eids: {
'fabrickId': {
source: 'neustar.biz',
atype: 1
},
}
};
function _getRefererInfo(configParams) {
if (configParams.refererInfo) {
return configParams.refererInfo;
} else {
return getRefererInfo();
}
}
function _getBaseUrl(configParams) {
if (configParams.url) {
return configParams.url;
} else {
return `https://fid.agkn.com/f?`;
}
}
function _setReferrer(refs, s) {
if (s) {
// store the longest one for the same URI
const url = s.split('?')[0];
// OR store the longest one for the same domain
// const url = s.split('?')[0].replace('http://','').replace('https://', '').split('/')[0];
if (refs.has(url)) {
const prevRef = refs.get(url);
if (s.length > prevRef.length) {
refs.set(url, s);
}
} else {
refs.set(url, s);
}
}
}
export function appendUrl(url, paramName, s, configParams) {
const maxUrlLen = (configParams && configParams.maxUrlLen) || 2000;
const maxRefLen = (configParams && configParams.maxRefLen) || 1000;
const maxSpaceAvailable = (configParams && configParams.maxSpaceAvailable) || 50;
// make sure we have enough space left to make it worthwhile
if (s && url.length < (maxUrlLen - maxSpaceAvailable)) {
let thisMaxRefLen = maxUrlLen - url.length;
if (thisMaxRefLen > maxRefLen) {
thisMaxRefLen = maxRefLen;
}
s = `&${paramName}=${encodeURIComponent(s)}`;
if (s.length >= thisMaxRefLen) {
s = s.substring(0, thisMaxRefLen);
if (s.charAt(s.length - 1) === '%') {
s = s.substring(0, thisMaxRefLen - 1);
} else if (s.charAt(s.length - 2) === '%') {
s = s.substring(0, thisMaxRefLen - 2);
}
}
return `${url}${s}`;
} else {
return url;
}
}
submodule('userId', fabrickIdSubmodule);