-
Notifications
You must be signed in to change notification settings - Fork 7
/
track.js
91 lines (74 loc) · 2.71 KB
/
track.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
const request = require('request')
const LOGGLY_KEY = '' // replace with your loggly key
const TAG = 'simple-track' // can replace with a tag of your choice
const ENDPOINT = `http://logs-01.loggly.com/inputs/${LOGGLY_KEY}/tag/${TAG}/`
// Domains to whitelist. Replace with your own!
const originWhitelist = [] // keep this empty and append domains to whitelist using whiteListDomain()
whitelistDomain('test.com')
whitelistDomain('nfeld.com')
function whitelistDomain(domain, addWww = true) {
const prefixes = [
'https://',
'http://',
]
if (addWww) {
prefixes.push('https://www.')
prefixes.push('http://www.')
}
prefixes.forEach(prefix => originWhitelist.push(prefix + domain))
}
function track(event, done) {
// event.queryStringParameters for querystring params already parsed into object
const trackerData = JSON.parse(event.body) // data from simple-tracker request
const headers = event.headers || {}
const ip = headers['x-forwarded-for'] || headers['x-bb-ip'] || '' // ip address of user incase you want to append to trackerData. I do it below.
console.info('tracker payload:', event.body)
console.info('ip:', ip)
// attach ip to context
if (trackerData && trackerData.context) {
trackerData.context.ip = ip
}
const reqOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
json: true,
url: ENDPOINT,
body: trackerData,
}
request(reqOptions, (error, result) => {
if (error) {
console.info('loggly error!', error)
} else {
console.info('result from loggly:', result.statusCode, result.statusMessage)
}
})
done()
}
exports.handler = function(event, context, callback) {
const origin = event.headers['origin'] || event.headers['Origin'] || ''
console.log(`Received ${event.httpMethod} request from, origin: ${origin}`)
const isOriginWhitelisted = originWhitelist.indexOf(origin) >= 0
console.info('is whitelisted?', isOriginWhitelisted)
const headers = {
//'Access-Control-Allow-Origin': '*', // allow all domains to POST. Use for localhost development only
'Access-Control-Allow-Origin': isOriginWhitelisted ? origin : originWhitelist[0],
'Access-Control-Allow-Methods': 'POST,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type,Accept',
}
const done = () => {
callback(null, {
statusCode: 200,
headers,
body: '',
})
}
if (event.httpMethod === 'OPTIONS') { // CORS (required if you use a different subdomain to host this function, or a different domain entirely)
done()
} else if (event.httpMethod !== 'POST' || !isOriginWhitelisted) { // allow POST request from whitelisted domains
callback('Not found')
} else {
track(event, done)
}
}