-
Notifications
You must be signed in to change notification settings - Fork 29
/
adtech.js
386 lines (328 loc) Β· 13.1 KB
/
adtech.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const functions = require('firebase-functions')
const express = require('express')
const cookieParser = require('cookie-parser')
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` })
const path = require('path')
const { createHash } = require('node:crypto')
const adtech = express()
adtech.use(express.json())
adtech.use(cookieParser())
adtech.set('view engine', 'pug')
adtech.set('views', './views/adtech')
const adtechUrl = process.env.ADTECH_URL
const advertiserUrl = process.env.ADVERTISER_URL
adtech.get('/', (req, res) => {
res.render('index')
})
/* -------------------------------------------------------------------------- */
/* Logging */
/* -------------------------------------------------------------------------- */
function log(...args) {
console.log('\x1b[45m%s\x1b[0m', '[from adtech server] ', ...args)
}
/* -------------------------------------------------------------------------- */
/* Key helper functions */
/* -------------------------------------------------------------------------- */
const SCALING_FACTOR_PURCHASE_COUNT = 32768
const SCALING_FACTOR_PURCHASE_VALUE = 22
function createHashAs64BitHex(input) {
return createHash('sha256').update(input).digest('hex').substring(0, 16)
}
function generateSourceKeyPiece(input) {
const hash = createHashAs64BitHex(input)
return `0x${hash}0000000000000000`
}
function generateTriggerKeyPiece(input) {
const hash = createHashAs64BitHex(input)
return `0x0000000000000000${hash}`
}
/* -------------------------------------------------------------------------- */
/* Debugging setup */
/* -------------------------------------------------------------------------- */
adtech.use(function (req, res, next) {
console.log(
'Time:',
Date.now(),
' ',
req.originalUrl,
' Cookies: ',
req.cookies
)
var headers = []
const legacyMeasurementCookie = req.cookies['__session']
if (legacyMeasurementCookie === undefined) {
const cookieValue = Math.floor(Math.random() * 1000000000000000)
headers.push(`__session=${cookieValue}; SameSite=None; Secure; HttpOnly`)
}
// Set the Attribution Reporting debug cookie
const debugCookie = req.cookies['ar_debug']
if (debugCookie === undefined) {
headers.push('ar_debug=1; SameSite=None; Secure; HttpOnly')
}
if (headers.length > 0) {
res.set('Set-Cookie', headers)
}
next()
})
/* -------------------------------------------------------------------------- */
/* Ad serving */
/* -------------------------------------------------------------------------- */
adtech.get('/ad-click', (req, res) => {
res.render('ad-click')
})
adtech.get('/ad-click-js', (req, res) => {
const href = `${process.env.ADVERTISER_URL}`
res.render('ad-click-js', {
href,
attributionsrc: `${adtechUrl}/register-source-js`
})
})
adtech.get('/ad-view-img', (req, res) => {
res.render('ad-view-img')
})
adtech.get('/ad-script-view-img', (req, res) => {
res.set('Content-Type', 'text/javascript')
const adUrl = `${process.env.ADTECH_URL}/ad-view-img`
const iframe = `<iframe src='${adUrl}' allow='attribution-reporting' width=190 height=190 scrolling=no frameborder=1 padding=0></iframe>`
res.send(`document.write("${iframe}");`)
})
adtech.get('/ad-script-click-element', (req, res) => {
res.set('Content-Type', 'text/javascript')
const adClickUrl = `${process.env.ADTECH_URL}/ad-click`
const iframe = `<iframe src='${adClickUrl}' allow='attribution-reporting' width=190 height=190 scrolling=no frameborder=1 padding=0></iframe>`
res.send(`document.write("${iframe}");`)
})
adtech.get('/ad-script-click-js', (req, res) => {
res.set('Content-Type', 'text/javascript')
const adClickNoLinkUrl = `${process.env.ADTECH_URL}/ad-click-js`
const iframe = `<iframe src='${adClickNoLinkUrl}' allow='attribution-reporting' width=190 height=190 scrolling=no frameborder=1 padding=0></iframe>`
res.send(`document.write("${iframe}");`)
})
/* -------------------------------------------------------------------------- */
/* Source registration (ad click or view) */
/* -------------------------------------------------------------------------- */
adtech.get(
['/register-source-js', '/register-source-image', '/register-source-href'],
(req, res) => {
const attributionDestination = process.env.ADVERTISER_URL
// For demo purposes, sourceEventId is a random ID. In a real system, this ID would be tied to a unique serving-time identifier mapped to any information an adtech provider may need
const sourceEventId = Math.floor(Math.random() * 1000000000000000)
const legacyMeasurementCookie = req.cookies['__session']
const headerConfig = {
source_event_id: `${sourceEventId}`,
destination: attributionDestination,
// Optional: expiry of 7 days (default is 30)
expiry: '604800',
// debug_key as legacyMeasurementCookie is a simple approach for demo purposes. In a real system, you may make debug_key a unique ID, and map it to additional source-time information that you deem useful for debugging or performance comparison.
debug_key: legacyMeasurementCookie,
filter_data: {
conversion_product_type: ['category_1']
},
aggregation_keys: {
purchaseCount: generateSourceKeyPiece('COUNT, CampaignID=12, GeoID=7'),
purchaseValue: generateSourceKeyPiece('VALUE, CampaignID=12, GeoID=7')
},
debug_reporting: true
}
// Send a response with the header Attribution-Reporting-Register-Source in order to instruct the browser to register a source event
res.set(
'Attribution-Reporting-Register-Source',
JSON.stringify(headerConfig)
)
log('REGISTERING SOURCE \n', headerConfig)
if (req.originalUrl === '/register-source-image') {
// Send back the response
res.status(200).sendFile('blue-shoes.png', {
root: path.join(__dirname, '../../sites/adtech')
})
} else if (req.originalUrl === '/register-source-js') {
res.status(200).send('OK')
} else if (req.originalUrl === '/register-source-href') {
res.redirect(advertiserUrl)
}
}
)
/* -------------------------------------------------------------------------- */
/* Attribution trigger (conversion) */
/* -------------------------------------------------------------------------- */
const CHECKOUT_COMPLETED = 'checkout-completed'
const ADD_TO_CART = 'add-to-cart'
const VISIT_PRODUCT_PAGE = 'visit-product-page'
const SIGNUP_NEWSLETTER = 'signup-newsletter'
const conversionValues = {
// Trigger data for views (event sources) must be 0 or 1 (1 bit)
// Trigger data for clicks (event sources) must be a value between 0 and 7 (3 bits)
// Checkout = 1, so that the value is consistent across clicks and views
[CHECKOUT_COMPLETED]: 1,
[ADD_TO_CART]: 2,
[VISIT_PRODUCT_PAGE]: 3,
[SIGNUP_NEWSLETTER]: 4
}
function getTriggerData(conversionType) {
return conversionValues[conversionType]
}
function getPriority(conversionType, usePriorities) {
if (!usePriorities) {
// No conversion should be prioritized specifically => always return a priority of 0
return 0
} else {
// Assign a priority of 100 to checkouts, and of 0 to other conversion types
return conversionType === CHECKOUT_COMPLETED ? 100 : 0
}
}
adtech.get('/conversion', (req, res) => {
const conversionType = req.query['conversion-type']
const isConversionAPurchase = conversionType === CHECKOUT_COMPLETED
const productCategory = req.query['product-category']
const purchaseValue = req.query['purchase-value']
const triggerData = getTriggerData(conversionType)
const usePriorities = req.query['prio-checkout'] === 'true'
const priority = getPriority(conversionType, usePriorities)
// Use the purchase ID as a deduplication key, since we only want to count purchases with the same ID once
const deduplicationKey = req.query['purchase-id']
// Use deduplication only if it's on in the app settings and if a deduplication key is presents
const useDeduplication = !!(deduplicationKey && req.query['dedup'] === 'true')
const filters = {
// Because conversion_product_type has been set to category_1 in the header Attribution-Reporting-Register-Source, any incoming conversion whose productCategory does not match category_1 will be filtered out i.e. will not generate a report.
conversion_product_type: [productCategory]
}
const eventTriggerData = [
{
trigger_data: `${triggerData}`,
// if priorities are on, specify the priority
...(usePriorities && { priority: `${priority}` }),
// if deduplication is on, specify the deduplication key
...(useDeduplication && { deduplication_key: deduplicationKey })
}
]
const aggregatableTriggerData = [
// Each dict independently adds pieces to multiple source keys.
{
key_piece: generateTriggerKeyPiece(`ProductCategory=${productCategory}`),
// Apply this key piece to:
source_keys: ['purchaseCount', 'purchaseValue']
}
]
const aggregatableValues = {
purchaseCount: 1 * SCALING_FACTOR_PURCHASE_COUNT,
purchaseValue: parseInt(purchaseValue) * SCALING_FACTOR_PURCHASE_VALUE
}
// Debug report (common to event-level and aggregate)
console.log('Conversion Cookies Set: ', req.cookies)
// Optional: set a debug key, and give it the value of the legacy measurement 3P cookie.
// This is a simple approach for demo purposes. In a real system, you would make this key a unique ID, and you may map it to additional trigger-time information that you deem useful for debugging or performance comparison.
const legacyMeasurementCookie = req.cookies['__session']
const headerConfig = {
filters: filters,
event_trigger_data: eventTriggerData,
debug_key: `${legacyMeasurementCookie}`,
debug_reporting: true
}
if (isConversionAPurchase) {
headerConfig.aggregatable_trigger_data = aggregatableTriggerData
headerConfig.aggregatable_values = aggregatableValues
}
res.set(
'Attribution-Reporting-Register-Trigger',
JSON.stringify(headerConfig)
)
res.sendStatus(200)
})
/* -------------------------------------------------------------------------- */
/* Reports */
/* -------------------------------------------------------------------------- */
adtech.get('/reports', (req, res) => {
res.send(JSON.stringify(reports))
})
// Event-level reports
adtech.post(
'/.well-known/attribution-reporting/report-event-attribution',
async (req, res) => {
console.log(
'\x1b[1;31m%s\x1b[0m',
`π Adtech has received an event-level report from the browser`
)
console.log(
'REGULAR REPORT RECEIVED (event-level):\n=== \n',
req.body,
'\n=== \n'
)
res.sendStatus(200)
}
)
// Primary debug reports for event-level
adtech.post(
'/.well-known/attribution-reporting/debug/report-event-attribution',
async (req, res) => {
console.log(
'\x1b[1;31m%s\x1b[0m',
`π Adtech has received a primary debug report for event-level from the browser`
)
console.log(
'DEBUG REPORT RECEIVED (event-level):\n=== \n',
req.body,
'\n=== \n'
)
res.sendStatus(200)
}
)
// Aggregatable reports
adtech.post(
'/.well-known/attribution-reporting/report-aggregate-attribution',
async (req, res) => {
console.log(
'\x1b[1;31m%s\x1b[0m',
`π Adtech has received an aggregatable report from the browser`
)
console.log(
'REGULAR REPORT RECEIVED (aggregate):\n=== \n',
req.body,
'\n=== \n'
)
res.sendStatus(200)
}
)
// Primary debug reports for aggregatable
adtech.post(
'/.well-known/attribution-reporting/debug/report-aggregate-attribution',
async (req, res) => {
console.log(
'\x1b[1;31m%s\x1b[0m',
`π Adtech has received a primary debug report for aggregatable from the browser`
)
console.log(
'DEBUG REPORT RECEIVED (aggregate):\n=== \n',
req.body,
'\n=== \n'
)
res.sendStatus(200)
}
)
// Verbose debug reports
adtech.post(
'/.well-known/attribution-reporting/debug/verbose',
async (req, res) => {
console.log(
'\x1b[1;31m%s\x1b[0m',
`π Adtech has received one or more verbose debug reports from the browser`
)
console.log('VERBOSE REPORT(S) RECEIVED:\n=== \n', req.body, '\n=== \n')
res.sendStatus(200)
}
)
exports.adtech = functions.https.onRequest(adtech)