Datadog custom check for collecting Google Analytics Real Time data.
This custom check allows you to retrieve Google Analytics information from the Real Time API and send it as a regular metric to Datadog.
- Support for multiple profiles (views)
- Handles Active users (rt:activeUsers) and Pageviews (rt:pageviews) metrics
- Allows for custom tagging for each profile
- Allows for dimension-tag mapping for pageviews metric. Example: adding rt:country dimension, generate the #country: tag.
In order for the integration to work you must create a Service Account, obtain it's API key and give permission to access Analytics API by adding the Service Account e-mail to the Authorised users in that Property. If you are ready with this, go directly to Step 3: Installing the check.
- Create a project
- Go to https://console.developers.google.com
- Upper right > Select a project
- Create a project or select an existing one
- Enable Google Analytics API
- In the left menu "API Manager", go to Overview
- In the search box type "analytics"
- Click on Analytics API
- Enable
- Create Access Credentials
- In the left menu "API Manager", go to Credentials
- Create credential > Service Account keys
- Select "New service account" and type in the name of it
- Select json for key file
- When done, don't forget to save the key json file
- Take note of service account email (ie: my-svc-account@my-project.12345.iam.gserviceaccount.com)
- Go into https://analytics.google.com and log in
- Go to Admin option in header menu
- Select the account you want to integrate. You may also select the specific property you want to give access.
- Goto to User Management
- Add permissions for the service account email address (Read & Analyze)
- Select the property and the view you wish to collect metrics from
- Take note of the View ID for later (ie: 12345678)
This steps are based on Ubuntu Linux.
- Clone or download from https://github.com/bithauschile/datadog-ga
- Install python libraries
- Use pip to install the Google API client for Python:
/opt/datadog-agent/embedded/bin/pip install --upgrade google-api-python-client
- Install the check:
- Copy ga.yaml to /etc/datadog-agent/conf.d/
- Copy ga.py to /etc/datadog-agent/checks.d/
- Copy the api key json file to the server in a directory the agent can access (ie: /etc/datadog-agent/conf.d/)
- Configure by adding the account information and the properties (views) you want to integrate in
/etc/datadog-agent/conf.d/ga.yaml
.
- In the following example, the query divides the pageviews in 3 dimensions (country, city, device). For more information about available dimensions go to this page.
- Be carefull with the min_collection_interval paramter. Google Analytics generate a by-minute result in the Real Time response with no timestamp. The only way to correlate Analytics data with the other metrics is running every 60 seconds aprox.
init_config: min_collection_interval: 55 service_account_email: my-svc-account@my-project.12345.iam.gserviceaccount.com key_file_location: /etc/datadog-agent/conf.d/key.json instances: - profile: ga:123456789 tags: - env:test pageview_dimensions: - rt:country - rt:city - rt:deviceCategory
Check that everything went right:
/etc/init.d/datadog-agent check ga
.
- Restart the agent:
/etc/init.d/datadog-agent restart
.
##References
- Google Analytics Real Time API
- Google API Explorer - Analytics Real Time
- Datadog - Writing an Agent Check
##Licence The code is licensed under the MIT License.