forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaveggIdSystem.js
127 lines (119 loc) · 3.64 KB
/
naveggIdSystem.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
/**
* This module adds naveggId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/naveggId
* @requires module:modules/userId
*/
import { isStr, isPlainObject, logError } from '../src/utils.js';
import { submodule } from '../src/hook.js';
import { ajaxBuilder } from '../src/ajax.js';
import { getStorageManager } from '../src/storageManager.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
*/
const MODULE_NAME = 'naveggId';
const OLD_NAVEGG_ID = 'nid';
const NAVEGG_ID = 'nvggid';
const BASE_URL = 'https://id.navegg.com/uid/';
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});
function getIdFromAPI() {
const resp = function (callback) {
ajaxBuilder()(
BASE_URL,
response => {
if (response) {
let responseObj;
try {
responseObj = JSON.parse(response);
} catch (error) {
logError(error);
const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie();
callback(fallbackValue);
}
if (responseObj && responseObj[NAVEGG_ID]) {
callback(responseObj[NAVEGG_ID]);
} else {
const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie();
callback(fallbackValue);
}
}
},
error => {
logError('Navegg ID fetch encountered an error', error);
const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie();
callback(fallbackValue);
},
{method: 'GET', withCredentials: false});
};
return resp;
}
/**
* @returns {string | null}
*/
function readNvgIdFromCookie() {
return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nvg') ? storage.findSimilarCookies('nvg')[0] : null) : null;
}
/**
* @returns {string | null}
*/
function readNavIdFromCookie() {
return storage.cookiesAreEnabled() ? (storage.findSimilarCookies('nav') ? storage.findSimilarCookies('nav')[0] : null) : null;
}
/**
* @returns {string | null}
*/
function readOldNaveggIdFromCookie() {
return storage.cookiesAreEnabled() ? storage.getCookie(OLD_NAVEGG_ID) : null;
}
/**
* @returns {string | null}
*/
function getOldCookie() {
const oldCookie = readOldNaveggIdFromCookie() || readNvgIdFromCookie() || readNavIdFromCookie();
return oldCookie;
}
/**
* @returns {string | null}
*/
function getNaveggIdFromLocalStorage() {
return storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage(NAVEGG_ID) : null;
}
/** @type {Submodule} */
export const naveggIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* decode the stored id value for passing to bid requests
* @function
* @param { Object | string | undefined } value
* @return { Object | string | undefined }
*/
decode(value) {
const naveggIdVal = value ? isStr(value) ? value : isPlainObject(value) ? value.id : undefined : undefined;
return naveggIdVal ? {
'naveggId': naveggIdVal.split('|')[0]
} : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} config
* @return {{id: string | undefined } | undefined}
*/
getId(config, consentData) {
const resp = getIdFromAPI()
return {callback: resp}
},
eids: {
'naveggId': {
source: 'navegg.com',
atype: 1
},
}
};
submodule('userId', naveggIdSubmodule);