A Python implementation of Google Analytics Measurement Protocol
Transaction handling depends on the prices
library.
Google strongly encourages using UUID version 4 as unique user handles. It's up to you to generate and persist the ID between actions, just make sure that all actions performed by the same user are reported using the same client ID.
import uuid
client_id = uuid.uuid4()
There are two ways to construct a PageView
object:
PageView(path[, host_name=None][, title=None][, referrer=None])
PageView(location='http://example.com/my-page/?foo=1'[, title=None][, referrer=None])
Example:
from google_measurement_protocol import PageView, report
view = PageView(path='/my-page/', title='My Page', referrer='http://example.com/')
report('UA-123456-1', client_id, view)
Use the Event
object:
Event('category', 'action'[, label=None][, value=None])
Example:
from google_measurement_protocol import Event, report
event = Event('profile', 'user_registered')
report('UA-123456-1', client_id, event)
First create Item
s to describe the contents of the transaction:
Item(name, unit_price[, quantity=None][, item_id=None])
Then the Transaction
itself:
Transaction(transaction_id, items[, revenue=None][, shipping=None][, affiliation=None])
If revenue
is given, it will override the total that is otherwise calculated
from items and shipping.
Example:
from google_measurement_protocol import Item, report, Transaction
from prices import Price
transaction_id = '0001' # any string should do
items = [Item('My awesome product', Price(90, currency='EUR'), quantity=2),
Item('Another product', Price(30, currency='EUR'))]
transaction = Transaction(transaction_id, items)
report('UA-123456-1', client_id, transaction)
You can pass extra_info
and extra_headers
to report()
function to submit
additional information.
extra_headers
is passed directly as additional headers to requests
library. This is currently the only way to pass User-Agent
.
extra_info
should be an instance of SystemInfo
. Currently only language
reporting is supported:
ga_extra
should be a dictionary of GA supported parameters. See https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
SystemInfo([language=None])
Example:
from google_measurement_protocol import PageView, report, SystemInfo
view = PageView(path='/my-page/', title='My Page', referrer='http://example.com/')
headers = {'user-agent': 'my-user-agent 1.0'}
info = SystemInfo(language='en-us')
extra = { 'uid': 12345678,
'uip': '123.34.45.678',
}
report('UA-123456-1', client_id, view, extra_info=info, extra_header=headers, ga_extra=extra)