forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowthCodeRtdProvider.js
136 lines (120 loc) · 3.92 KB
/
growthCodeRtdProvider.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
/**
* This module adds GrowthCode HEM and other Data to Bid Requests
* @module modules/growthCodeRtdProvider
*/
import { submodule } from '../src/hook.js'
import { getStorageManager } from '../src/storageManager.js';
import {
logMessage, logError, mergeDeep
} from '../src/utils.js';
import * as ajax from '../src/ajax.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
import {tryAppendQueryString} from '../libraries/urlUtils/urlUtils.js';
const MODULE_NAME = 'growthCodeRtd';
const LOG_PREFIX = 'GrowthCodeRtd: ';
const ENDPOINT_URL = 'https://p2.gcprivacy.com/v2/rtd?'
const RTD_EXPIRE_KEY = 'gc_rtd_expires_at'
const RTD_CACHE_KEY = 'gc_rtd_items'
export const storage = getStorageManager({ moduleType: MODULE_TYPE_RTD, moduleName: MODULE_NAME });
let items
export const growthCodeRtdProvider = {
name: MODULE_NAME,
init: init,
getBidRequestData: alterBidRequests,
addData: addData,
callServer: callServer
};
/**
* Parse json if possible, else return null
* @param data
* @returns {any|null}
*/
function tryParse(data) {
try {
return JSON.parse(data);
} catch (err) {
logError(err);
return null;
}
}
/**
* Init The RTD Module
* @param config
* @param userConsent
* @returns {boolean}
*/
function init(config, userConsent) {
logMessage(LOG_PREFIX + 'Init RTB');
if (config == null) {
return false
}
const configParams = (config && config.params) || {};
let expiresAt = parseInt(storage.getDataFromLocalStorage(RTD_EXPIRE_KEY, null));
items = tryParse(storage.getDataFromLocalStorage(RTD_CACHE_KEY, null));
if (configParams.pid === undefined) {
return true; // Die gracefully
} else {
return callServer(configParams, items, expiresAt, userConsent);
}
}
function callServer(configParams, items, expiresAt, userConsent) {
// Expire Cache
let now = Math.trunc(Date.now() / 1000);
if ((!isNaN(expiresAt)) && (now > expiresAt)) {
expiresAt = NaN;
storage.removeDataFromLocalStorage(RTD_CACHE_KEY, null)
storage.removeDataFromLocalStorage(RTD_EXPIRE_KEY, null)
}
if ((items === null) && (isNaN(expiresAt))) {
let gcid = storage.getDataFromLocalStorage('gcid')
let url = configParams.url ? configParams.url : ENDPOINT_URL;
url = tryAppendQueryString(url, 'pid', configParams.pid);
url = tryAppendQueryString(url, 'u', window.location.href);
url = tryAppendQueryString(url, 'gcid', gcid);
if ((userConsent !== null) && (userConsent.gdpr !== null) && (userConsent.gdpr.consentString)) {
url = tryAppendQueryString(url, 'tcf', userConsent.gdpr.consentString)
}
ajax.ajaxBuilder()(url, {
success: response => {
let respJson = tryParse(response);
// If response is a valid json and should save is true
if (respJson && respJson.results >= 1) {
storage.setDataInLocalStorage(RTD_CACHE_KEY, JSON.stringify(respJson.items), null);
storage.setDataInLocalStorage(RTD_EXPIRE_KEY, respJson.expires_at, null)
} else {
storage.setDataInLocalStorage(RTD_EXPIRE_KEY, respJson.expires_at, null)
}
},
error: error => {
logError(LOG_PREFIX + 'ID fetch encountered an error', error);
}
}, undefined, {method: 'GET', withCredentials: true})
}
return true;
}
function addData(reqBidsConfigObj, items) {
let merge = false
for (let j = 0; j < items.length; j++) {
let item = items[j]
let data = JSON.parse(item.parameters);
if (item['attachment_point'] === 'data') {
mergeDeep(reqBidsConfigObj.ortb2Fragments.bidder, data)
merge = true
}
}
return merge
}
/**
* Alter the Bid Request for additional information such as HEM or 3rd Party Ids
* @param reqBidsConfigObj
* @param callback
* @param config
* @param userConsent
*/
function alterBidRequests(reqBidsConfigObj, callback, config, userConsent) {
if (items != null) {
addData(reqBidsConfigObj, items)
}
callback();
}
submodule('realTimeData', growthCodeRtdProvider);