forked from bithauschile/datadog-ga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
123 lines (97 loc) · 4.03 KB
/
ga.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
"""
Google Analytics check
Collects metrics from the Analytics API.
Jonathan Makuc - Bithaus Chile (Datadog Partner) - [email protected]
2016-04-13
- Support for pageViews metric
- Metric value is read from "1 minute ago" instead of "during the last minute"
in order to obtain a consistent value to report to Datadog. Using "during the last
minute" result in reading zeros while waiting for visitors to view pages in that time
frame.
- Dimensions and tags can be controlled on yaml file
"""
# the following try/except block will make the custom check compatible with any Agent version
try:
# first, try to import the base class from old versions of the Agent...
from checks import AgentCheck, CheckException
except ImportError:
# ...if the above failed, the check is running in Agent version 6 or later
from datadog_checks.checks import AgentCheck, CheckException
from google.oauth2 import service_account
import googleapiclient.discovery
class GoogleAnalyticsCheck(AgentCheck):
""" Collects as many metrics as instances defined in ga.yaml
"""
scope = ['https://www.googleapis.com/auth/analytics.readonly']
service = 0
apiName = 'analytics'
version = 'v3'
def check(self, instance):
self.log.info('profile: %s, tags: %s, pageview_dimensions: %s' % (instance.get('profile'), instance.get('tags'), instance.get('pageview_dimensions')))
profile = instance.get('profile')
instanceTags = instance.get('tags')
instanceTags.append("profile:" + profile)
# pageview collection
metricName = 'rt:pageviews'
pageviewsDims = ['rt:minutesAgo']
confDims = instance.get('pageview_dimensions');
if isinstance(confDims, list):
pageviewsDims = pageviewsDims + confDims
result = self.get_results(profile, metricName, pageviewsDims)
headers = result.get('columnHeaders')
rows = result.get('rows')
if len(rows) < 1:
return
pvMetricsSent = 0
for row in rows:
# In order to have a consistent metric, we look for the value 1 minute ago
# and not during the last minute.
if int(row[0]) == 1:
tags = []
tags.extend(instanceTags)
for i in xrange(len(headers)-1):
if i > 0:
# we remove the "rt" from the dimension name
tags.append(headers[i].get('name')[3:] + ":" + row[i])
self.gauge("googleanalytics.rt.pageviews",
row[len(row)-1],
tags=tags,
hostname=None,
device_name=None)
pvMetricsSent = pvMetricsSent + 1
self.log.info("Pageview Metrics sent %s" % pvMetricsSent)
# activeUsers collection
metricName = 'rt:activeUsers'
activeuserDims = []
tags = []
tags.extend(instanceTags)
result = self.get_results(profile, metricName, activeuserDims)
activeUsers = int(result.get("totalsForAllResults").get(metricName))
self.gauge("googleanalytics.rt.activeUsers",
activeUsers,
tags=tags,
hostname=None,
device_name=None)
self.log.info("Active users %s" % activeUsers);
def __init__(self, *args, **kwargs):
AgentCheck.__init__(self, *args, **kwargs)
self.log.info('key_file_location: %s' % self.init_config.get('key_file_location'))
self.service = self.get_service(
self.apiName,
self.version,
self.scope,
self.init_config.get('key_file_location'))
def get_service(self, api_name, api_version, scope, key_file_location):
credentials = service_account.Credentials.from_service_account_file(key_file_location, scopes=scope)
service = googleapiclient.discovery.build(api_name, api_version, credentials=credentials)
return service
def get_results(self, profile_id, the_metric, dims):
if len(dims) > 0:
return self.service.data().realtime().get(
ids=profile_id,
metrics=the_metric,
dimensions=','.join(dims)).execute()
else:
return self.service.data().realtime().get(
ids=profile_id,
metrics=the_metric).execute()