forked from brave/metric-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (242 loc) · 8.09 KB
/
index.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
"use strict"
// Dependencies
// ===
const BodyParser = require('body-parser')
const CookieParser = require("cookie-parser")
// web server
const Express = require("express")
// better than debugger
const Pry = require("pryjs")
// HTTP requests
const Request = require("request")
// Logging
const Winston = require("winston")
// Parsing query
const Querystring = require('querystring')
// Config
// ===
const MIXPANEL_API_HOST = process.env.MIXPANEL_API_HOST || "https://api.mixpanel.com"
// e.g. token1234a,token5678b
const MIXPANEL_TOKEN_WHITELIST = process.env.MIXPANEL_TOKEN_WHITELIST.split(",")
const NODE_ENV = process.env.NODE_ENV || "development"
const LOG_LEVEL = process.env.LOG_LEVEL || (NODE_ENV === "production" ? "info" : "debug")
const COOKIE_PERSISTED_PARAMS = process.env.COOKIE_PERSISTED_PARAMS
? process.env.COOKIE_PERSISTED_PARAMS.split(",")
: ["campaign", "creative", "placement", "referer", "utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term"]
const PERSISTED_COOKIE_NAME = "metricProxy"
const COOKIE_DOMAIN = process.env.COOKIE_DOMAIN
const COOKIE_SECURE_ATTRIBUTE = (NODE_ENV === "production")
const COOKIE_SIGNING_SECRET = NODE_ENV === "production"
? process.env.COOKIE_SIGNING_SECRET
: null
const COOKIE_TTL = 14 * 24 * 60 * 60 * 1000
const PORT = process.env.PORT || 4000
const USER_AGENT = `metric-proxy/${process.env.npm_package_version} (brave.com)`
// Lib
// ===
var logger = new (Winston.Logger)({
level: LOG_LEVEL,
transports: [
new (Winston.transports.Console)()
]
})
// Mixpanel query string includes:
// - Original request query string -- certain params
// - Certain other query params, moved from top level into query data.properties
// - Persisted params from previous requests, via cookies
function buildMixpanelTrackQueryString(request, response) {
if (!request.query.data) {
if (!request.body) {
throw "Query and body are missing"
}
// Fill query with body and make body empty as all further operations are done with query
request.query = Querystring.parse(request.body)
request.body = ""
}
const dataString = decodeURI(Buffer.from(request.query.data, "base64").toString("utf-8"))
// HACK: Handle mixpanel iOS Swift 2.x library which sends single quoted JSON.
const data = JSON.parse(dataString.replace(/'/g, "\""))
if (!isValidMixpanelTrackData(data)) {
throw "Invalid data"
}
let queryString = { data }
// /track supports other params but not sure if we want them
const queryStringParams = ["img", "verbose"]
queryStringParams.forEach((value) => {
if (!request.query[value]) {
return
}
queryString[value] = request.query[value]
})
// Copy special params from query string
COOKIE_PERSISTED_PARAMS.forEach((key) => {
if (!request.query[key]) {
return
}
queryString.data.properties[key] = request.query[key]
})
// Restore persisted params
const restoredParams = restorePersistedParams(request, response)
if (Object.keys(restoredParams).length > 0) {
for (let key in restoredParams) {
if (queryString.data.properties[key]) {
continue
}
queryString.data.properties[key] = restoredParams[key]
}
}
logger.debug("API > Query/data:", queryString.data)
const returnDataString = JSON.stringify(queryString.data)
queryString.data = Buffer.from(returnDataString, "utf-8").toString("base64")
return queryString
}
function isValidMixpanelToken(token) {
if (!token) {
return false
}
// HACK: Handle mixpanel iOS Swift 2.x library which sends token as "Optional({token})"
return MIXPANEL_TOKEN_WHITELIST.some((whitelistToken) => {
return token.includes(whitelistToken)
})
}
// Log additional things in development environments.
function debugLogger(request, response, next) {
logger.debug("-> Headers:", request.headers)
if (request.cookies) {
for (let cookieName in request.cookies) {
logger.debug(`-> Cookie: ${cookieName}:`, request.cookies[cookieName])
}
}
if (!request.query.data) {
// Fill query with body and make body empty as all further operations are done with query
request.query = Querystring.parse(request.body)
request.body = ""
}
if (request.query.data) {
logger.debug("-> Query:", request.query)
const dataString = decodeURI(Buffer.from(request.query.data, "base64").toString("utf-8"))
// HACK: Handle mixpanel iOS Swift 2.x library which sends single quoted JSON.
const data = JSON.parse(dataString.replace(/'/g, "\""))
logger.debug("-> Query data:", data)
} else {
logger.debug("-> Query: (empty)")
}
if (request.body) {
logger.debug("-> Body:", request.body)
// HACK: Handle mixpanel iOS Swift 2.x library which sends single quoted JSON.
const dataBody = JSON.parse(request.body.replace(/'/g, "\""))
logger.debug("-> Body data:", dataBody)
} else {
logger.debug("-> Body: (empty)")
}
next()
}
// Log Request responses from Mixpanel
function logRequestResponse(response) {
logger.debug("<<", response.statusCode)
logger.debug("<<", response.headers)
}
// Takes an example URL with tracking parameters and persists them to cookies.
// Alternative to the mixpanel library, which persists things like utm_* as
// Super Properties.
// Useful where JS is unavailable like tracking pixels or click redirects.
// Returns persisted params
function persistParamsAsCookies(request, response) {
let cookieParams = {}
COOKIE_PERSISTED_PARAMS.forEach((key) => {
if (!request.query[key]) {
return
}
cookieParams[key] = request.query[key]
})
if (Object.keys(cookieParams).length === 0) {
return {}
}
let options = {
httpOnly: true,
expires: new Date(Date.now() + COOKIE_TTL),
secure: COOKIE_SECURE_ATTRIBUTE,
signed: !!COOKIE_SIGNING_SECRET
}
if (COOKIE_DOMAIN) {
options["domain"] = COOKIE_DOMAIN
}
logger.debug(`<< Cookie: ${PERSISTED_COOKIE_NAME}:`, cookieParams)
response.cookie(PERSISTED_COOKIE_NAME, cookieParams, options)
return cookieParams
}
function restorePersistedParams(request, response) {
if (!request.cookies[PERSISTED_COOKIE_NAME]) {
return {}
}
// TODO: More logics
return request.cookies[PERSISTED_COOKIE_NAME]
}
// App fn
// ===
// https://mixpanel.com/help/reference/http
function mixpanelTrack(request, response) {
try {
logger.info(`-> ${request.method} /track`)
// Save certain query params across requests with cookies
persistParamsAsCookies(request, response)
const queryString = buildMixpanelTrackQueryString(request, response)
const mixpanelRequestOptions = {
headers: {
"User-Agent": USER_AGENT
},
qs: queryString
}
logger.debug(`API > ${request.method} ${MIXPANEL_API_HOST}/track`, mixpanelRequestOptions)
Request(`${MIXPANEL_API_HOST}/track`, mixpanelRequestOptions).
on("error", (error) => {
logger.error(error)
response.status(502).send("0")
}).
on("response", function(response) {
if (LOG_LEVEL === "debug") {
logRequestResponse(response)
}
}).
pipe(response)
} catch (_e) {
if (_e.stack) {
logger.error(_e.stack.split("\n").slice(0, 4).join(";"))
} else {
logger.error(_e)
}
response.send("0")
}
}
function isValidMixpanelTrackData(data) {
// Android MixpanelAPI doesn't pass these requirements
// data.event and data.properties are empty
// And it fails to get data.properties.token
return (true/*data.event && data.properties && isValidMixpanelToken(data.properties.token)*/)
}
// App
// ===
// Express setup
// ---
var express = Express()
express.disable("x-powered-by")
if (COOKIE_SIGNING_SECRET) {
express.use(CookieParser(COOKIE_SIGNING_SECRET))
} else {
express.use(CookieParser())
}
express.use(BodyParser.text({type: "*/*"}))
if (LOG_LEVEL === "debug") {
express.use(debugLogger)
}
// Routes
// ---
express.get("/", (request, response) => {
response.send("hello friend")
})
express.get("/track", mixpanelTrack)
express.post("/track", mixpanelTrack)
express.listen(PORT)
logger.info(`MIXPANEL_API_HOST: ${MIXPANEL_API_HOST}`)
logger.info(`NODE_ENV: ${NODE_ENV}`)
logger.info(`metric-proxy up on localhost:${PORT}`)