-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
enrichmentFpdModule.js
167 lines (141 loc) · 4.45 KB
/
enrichmentFpdModule.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
/**
* This module sets default values and validates ortb2 first part data
* @module modules/firstPartyData
*/
import { timestamp, mergeDeep } from '../src/utils.js';
import { submodule } from '../src/hook.js';
import { getRefererInfo } from '../src/refererDetection.js';
import { getCoreStorageManager } from '../src/storageManager.js';
let ortb2 = {};
let win = (window === window.top) ? window : window.top;
export const coreStorage = getCoreStorageManager('enrichmentFpd');
/**
* Find the root domain
* @param {string|undefined} fullDomain
* @return {string}
*/
export function findRootDomain(fullDomain = window.location.hostname) {
if (!coreStorage.cookiesAreEnabled()) {
return fullDomain;
}
const domainParts = fullDomain.split('.');
if (domainParts.length == 2) {
return fullDomain;
}
let rootDomain;
let continueSearching;
let startIndex = -2;
const TEST_COOKIE_NAME = `_rdc${Date.now()}`;
const TEST_COOKIE_VALUE = 'writeable';
do {
rootDomain = domainParts.slice(startIndex).join('.');
let expirationDate = new Date(timestamp() + 10 * 1000).toUTCString();
// Write a test cookie
coreStorage.setCookie(
TEST_COOKIE_NAME,
TEST_COOKIE_VALUE,
expirationDate,
'Lax',
rootDomain,
undefined
);
// See if the write was successful
const value = coreStorage.getCookie(TEST_COOKIE_NAME, undefined);
if (value === TEST_COOKIE_VALUE) {
continueSearching = false;
// Delete our test cookie
coreStorage.setCookie(
TEST_COOKIE_NAME,
'',
'Thu, 01 Jan 1970 00:00:01 GMT',
undefined,
rootDomain,
undefined
);
} else {
startIndex += -1;
continueSearching = Math.abs(startIndex) <= domainParts.length;
}
} while (continueSearching);
return rootDomain;
}
/**
* Checks for referer and if exists merges into ortb2 global data
*/
function setReferer() {
if (getRefererInfo().referer) mergeDeep(ortb2, { site: { ref: getRefererInfo().referer } });
}
/**
* Checks for canonical url and if exists merges into ortb2 global data
*/
function setPage() {
if (getRefererInfo().canonicalUrl) mergeDeep(ortb2, { site: { page: getRefererInfo().canonicalUrl } });
}
/**
* Checks for canonical url and if exists retrieves domain and merges into ortb2 global data
*/
function setDomain() {
let parseDomain = function(url) {
if (!url || typeof url !== 'string' || url.length === 0) return;
var match = url.match(/^(?:https?:\/\/)?(?:www\.)?(.*?(?=(\?|\#|\/|$)))/i);
return match && match[1];
};
let domain = parseDomain(getRefererInfo().canonicalUrl)
if (domain) {
mergeDeep(ortb2, { site: { domain: domain } });
mergeDeep(ortb2, { site: { publisher: { domain: findRootDomain(domain) } } });
};
}
/**
* Checks for screen/device width and height and sets dimensions
*/
function setDimensions() {
let width;
let height;
try {
width = win.innerWidth || win.document.documentElement.clientWidth || win.document.body.clientWidth;
height = win.innerHeight || win.document.documentElement.clientHeight || win.document.body.clientHeight;
} catch (e) {
width = window.innerWidth || window.document.documentElement.clientWidth || window.document.body.clientWidth;
height = window.innerHeight || window.document.documentElement.clientHeight || window.document.body.clientHeight;
}
mergeDeep(ortb2, { device: { w: width, h: height } });
}
/**
* Scans page for meta keywords, and if exists, merges into site.keywords
*/
function setKeywords() {
let keywords;
try {
keywords = win.document.querySelector("meta[name='keywords']");
} catch (e) {
keywords = window.document.querySelector("meta[name='keywords']");
}
if (keywords && keywords.content) mergeDeep(ortb2, { site: { keywords: keywords.content.replace(/\s/g, '') } });
}
/**
* Resets modules global ortb2 data
*/
const resetOrtb2 = () => { ortb2 = {} };
function runEnrichments() {
setReferer();
setPage();
setDomain();
setDimensions();
setKeywords();
return ortb2;
}
/**
* Sets default values to ortb2 if exists and adds currency and ortb2 setConfig callbacks on init
*/
export function initSubmodule(fpdConf, data) {
resetOrtb2();
return (!fpdConf.skipEnrichments) ? mergeDeep(runEnrichments(), data) : data;
}
/** @type {firstPartyDataSubmodule} */
export const enrichmentsSubmodule = {
name: 'enrichments',
queue: 2,
init: initSubmodule
}
submodule('firstPartyData', enrichmentsSubmodule)