Use Measurement Protocol in Python

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.

About Measurement Protocol

Official documentation

https://developers.google.com/analytics/devguides/collection/protocol/

Difference from data import

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

Usage example

Google Analytics Free Edition

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.

スクリーンショット 2019-12-08 22.47.26.png

Google Analytics Paid Edition + Google Cloud Platform

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.

スクリーンショット 2019-12-08 22.47.38.png

Although the introduction has been lengthened, the following is an example of using the Measurement Protocol.

environment

Machine / OS

$ 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

program

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

Execution method

$ python mp.py

Execution result

If you look at the real-time events in Google Analytics, you can see that the events you set are being tracked.

スクリーンショット 2019-12-09 0.41.01.png

Other irrelevant stories

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

Use Measurement Protocol in Python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Let's use def in python
Use let expression in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Read Protocol Buffers data in Python3
Use fabric as is in python (fabric3)
How to use SQLite in Python
Disable python2.6 ssl3 protocol in centos6
Use rospy with virtualenv in Python3
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Use OpenCV with Python 3 in Window
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Use print in a Python2 lambda expression
Meta-analysis in Python
Unittest in python
Easily use your own functions in Python
Epoch in Python
Discord in Python
python time measurement
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Easy way to use Wikipedia in Python
Plink in Python
Constant in python
Don't use \ d in Python 3 regular expressions!
Lifegame in Python.
FizzBuzz in Python
How to use __slots__ in Python class
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7