Skip to content

Commit

Permalink
add field tracking #110
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry committed Jul 28, 2021
1 parent e445072 commit bba25ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
12 changes: 8 additions & 4 deletions biothings/web/analytics/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def handles(self, event):
return isinstance(event, Event)

def send(self, payload):
yield HTTPRequest(
'http://www.google-analytics.com/batch', method='POST',
body='\n'.join(payload.to_GA_payload(self.tracking_id, self.uid_version))
)
events = payload.to_GA_payload(self.tracking_id, self.uid_version)
# #batch-limitations section of the URL above
# A maximum of 20 hits can be specified per request.
for i in range(0, len(events), 20):
yield HTTPRequest(
'http://www.google-analytics.com/batch', method='POST',
body='\n'.join(events[i: i+20])
)
14 changes: 12 additions & 2 deletions biothings/web/analytics/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def to_GA_payload(self, tracking_id, cid_version=1):
def __str__(self): # to facilitate logging
return f"{type(self).__name__}({pformat(self)})"

def _clean(dict):
return {k: v for k, v in dict.items() if v}

class GAEvent(Event):

# GA Event
Expand All @@ -127,7 +130,7 @@ def to_GA_payload(self, tracking_id, cid_version=1):

payloads = super().to_GA_payload(tracking_id, cid_version)
if self.get("category") and self.get("action"):
payloads.append(urlencode({
payloads.append(urlencode(_clean({
"v": 1, # protocol version
"t": "event",
"tid": tracking_id,
Expand All @@ -136,7 +139,14 @@ def to_GA_payload(self, tracking_id, cid_version=1):
"ea": self["action"],
"el": self.get("label", ""),
"ev": self.get("value", "")
}))
})))
for event in self.get("__secondary__", []):
event["__request__"] = self["__request__"]
payloads.extend(
event.to_GA_payload(
tracking_id, cid_version)[1:])
# ignore the first event (pageview)
# which is already generated once
return payloads


Expand Down
14 changes: 14 additions & 0 deletions biothings/web/handlers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,26 @@ def prepare(self):
self.args.biothing_type = self.biothing_type

self.event = GAEvent({
'__secondary__': [], # secondary analytical objective: field tracking
'category': '{}_api'.format(self.biothings.config.APP_VERSION), # eg.'v1_api'
'action': '_'.join((self.name, self.request.method.lower())), # eg.'query_get'
# 'label': 'fetch_all', etc.
# 'value': 100, # number of queries
})

if self.args._source:
for _source in self.args._source:
self.event['__secondary__'].append(GAEvent({
'category': 'field_filter',
'action': 'enabled',
'label': _source.split('.', 1)[0] # root key
}))
else:
self.event['__secondary__'].append(GAEvent({
'category': 'field_filter',
'action': 'disabled'
}))

def write(self, chunk):
# add an additional header to the JSON formatter
# with a header image and a title-like section
Expand Down

0 comments on commit bba25ac

Please sign in to comment.