diff --git a/biothings/web/analytics/channels.py b/biothings/web/analytics/channels.py index ac2ff46fe..4b7bdcab9 100644 --- a/biothings/web/analytics/channels.py +++ b/biothings/web/analytics/channels.py @@ -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]) + ) diff --git a/biothings/web/analytics/events.py b/biothings/web/analytics/events.py index ad1753ac6..178b4b128 100644 --- a/biothings/web/analytics/events.py +++ b/biothings/web/analytics/events.py @@ -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 @@ -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, @@ -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 diff --git a/biothings/web/handlers/query.py b/biothings/web/handlers/query.py index 9b177654d..6d2af1edf 100644 --- a/biothings/web/handlers/query.py +++ b/biothings/web/handlers/query.py @@ -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