forked from lnklnklnk/ga-bq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (118 loc) · 5.01 KB
/
main.py
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
#!/usr/bin/env python
import webapp2
import json
import string
import base64
from google.appengine.api import taskqueue
from urllib import urlencode
from urlparse import urlparse
from urlparse import parse_qs
import logging
import time
import re
class MainHandler(webapp2.RequestHandler):
params_mapping = {
'cid': 'clientId',
't': 'type',
'v': 'version',
'tid': 'tid',
'uid': 'userId',
'dr': 'trafficSource.referralPath',
'sr': 'device.screenResolution',
'vp': 'device.viewPort',
'de': 'device.encoding',
'sd': 'device.screenColors',
'ul': 'device.language',
'je': 'device.javaEnabled',
'fl': 'device.flashVersion',
'dl': 'page.location',
'dt': 'page.title',
'dh': 'page.hostname',
'dp': 'page.pagePath',
'ec': 'eventInfo.eventCategory',
'ea': 'eventInfo.eventAction',
'el': 'eventInfo.eventLabel',
'ev': 'eventInfo.eventValue',
}
def process_ga_params(self, params):
data = {}
if 'je' in params and params['je'] == '1':
params['je'] = True
else:
params['je'] = False
parsed_url = urlparse(params['dl'])
if 'dh' not in params or not params['dh']:
params['dh'] = parsed_url.hostname
if 'dp' not in params or not params['dp']:
params['dp'] = parsed_url.path
if parsed_url.query:
parsed_query_string = parse_qs(parsed_url.query)
if 'utm_source' in parsed_query_string:
if 'trafficSource' not in data:
data['trafficSource'] = {}
data['trafficSource']['source'] = parsed_query_string['utm_source'][0]
parsed_query_string.pop('utm_source', None)
if 'utm_medium' in parsed_query_string:
if 'trafficSource' not in data:
data['trafficSource'] = {}
data['trafficSource']['medium'] = parsed_query_string['utm_medium'][0]
parsed_query_string.pop('utm_medium', None)
if 'utm_campaign' in parsed_query_string:
if 'trafficSource' not in data:
data['trafficSource'] = {}
data['trafficSource']['campaign'] = parsed_query_string['utm_campaign'][0]
parsed_query_string.pop('utm_campaign', None)
if 'utm_term' in parsed_query_string:
if 'trafficSource' not in data:
data['trafficSource'] = {}
data['trafficSource']['term'] = parsed_query_string['utm_term'][0]
parsed_query_string.pop('utm_term', None)
if 'utm_content' in parsed_query_string:
if 'trafficSource' not in data:
data['trafficSource'] = {}
data['trafficSource']['content'] = parsed_query_string['utm_content'][0]
parsed_query_string.pop('utm_content', None)
if parsed_query_string:
params['dp'] += '?' + urlencode(parsed_query_string)
for key, value in self.params_mapping.iteritems():
if key in self.request.GET:
data = self.map_params(data, value, params[key])
a = re.compile("^cm([1-9][0-9]*)")
for key, value in self.request.GET.iteritems():
res = a.match(key)
if res:
if 'customMetrics' not in data:
data['customMetrics'] = []
data['customMetrics'].append({'index':res.group(1),'value':value})
a = re.compile("^cd([1-9][0-9]*)")
for key, value in self.request.GET.iteritems():
res = a.match(key)
if res:
if 'customDimensions' not in data:
data['customDimensions'] = []
data['customDimensions'].append({'index':res.group(1),'value':value})
data['device']['userAgent'] = self.request.headers.get('User-Agent')
data['timestamp'] = int(time.time())
data['ip']=self.request.remote_addr
return data
def get(self):
data = self.process_ga_params(self.request.GET)
q = taskqueue.Queue('pull-queue')
tasks = [taskqueue.Task(payload=json.dumps(data), method='PULL')]
logging.info(data)
q.add(tasks)
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Content-Type'] = 'image/gif'
self.response.write(base64.b64decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='))
def map_params(self, data, key, value):
str_arr = string.split(key, '.')
if len(str_arr) == 2:
if not (str_arr[0] in data):
data[str_arr[0]] = {}
data[str_arr[0]][str_arr[1]] = value
else:
data[str_arr[0]] = value
return data
app = webapp2.WSGIApplication([
('/collect', MainHandler)
], debug=True)