Hello everyone. @best_not_best.
By using the Google Analytics Measurement Protocol, you can send raw data directly to the Google Analytics server in an HTTP request. This allows you to connect your online and offline behavior and analyze it on Google Analytics.
https://developers.google.com/analytics/devguides/collection/protocol/
Google Analytics has a similar feature called Data Import (https://support.google.com/analytics/answer/3191589), but the differences are (roughly):
Measurement Protocol | Data import | |
---|---|---|
How to send data | HTTP request | Mainly file upload |
User to send | It is also possible to send data of users (new users) that do not exist in Google Analytics | Only users (existing users) existing in Google Analytics |
Reflection time | Near real time | It may take up to 24 hours |
Official documentation with usage examples in Python | None | Yes (Management API) |
Get a member ID in Custom Dimension. After combining with system data, data is transmitted by Measurement Protocol. The data sent to Google Analytics can also be used for advertisement distribution.
You can use BigQuery Export of Google Analytics data, so after combining with system data on BigQuery, data from Dataflow or Cloud Composer with Measurement Protocol I think it's better to send.
Although the introduction has been lengthened, the following is an example of using the Measurement Protocol.
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
Python
mp.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""send Google Analytics data using MeasurementProtocol."""
import sys
import urllib.parse
import urllib.request
from urllib.request import urlopen
if __name__ == '__main__':
mp_url = 'https://www.google-analytics.com/collect'
tid = 'UA-XXXXX-X'
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:71.0) Gecko/20100101 Firefox/71.0'
t = 'event'
ec = 'sample_event_category'
ea = 'sample_event_action'
el = 'sample_event_labe'
v = '1'
ni = 1
# url params
url_params = [
{
'v': v,
'tid': tid,
'ua': ua,
't': t,
'ec': ec,
'ea': ea,
'el': el,
'ni': '1',
'cid': '11111.11111',
},
{
'v': v,
'tid': tid,
'ua': ua,
't': t,
'ec': ec,
'ea': ea,
'el': el,
'ni': '1',
'cid': '22222.22222',
},
{
'v': v,
'tid': tid,
'ua': ua,
't': t,
'ec': ec,
'ea': ea,
'el': el,
'ni': '1',
'cid': '33333.33333',
},
]
try:
for url_param in url_params:
# url encode
data = urllib.parse.urlencode(url_param).encode('utf-8')
request = urllib.request.Request(mp_url, data)
response = urlopen(request)
except Exception as e:
print(e)
sys.exit(1)
sys.exit(0)
For tid
, specify the tracking ID of the destination Google Analytics, and for ʻua, specify an appropriate user agent. ʻUrl_params
is an API parameter. Please set each Google Analytics client ID in cid
.
See below for the parameters that can be used.
https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
$ python mp.py
If you look at the real-time events in Google Analytics, you can see that the events you set are being tracked.
After updating pyenv on Mac, openssl doesn't work well, so I'm running it in a cloud environment ... (That's why Python version is 3.5.) I'm sorry to fix it ...
Recommended Posts