A Python implementation of Google Analytics Measurement Protocol. Forked from https://github.com/mirumee/google-measurement-protocol and modified to only support user IDs, not client IDs.
Transaction handling depends on the prices library.
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', user_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', user_id, event)First create Items 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', user_id, transaction)For Extended Ecommerce we have implemented Purchase tracking, please note this will add an event automatically, as required by Google Analytics
First create EnhancedItems to describe the contents of the transaction:
EnhancedItem(name, unit_price[, quantity=None][, item_id=None]
[, category=None][, brand=None][, variant=None])Then the EnhancedPurchase itself:
EnhancedPurchase(transaction_id, items, url_page[, revenue=None][, tax=None]
[, shipping=None][, host=None][, affiliation=None])If revenue is given, it will override the total that is otherwise calculated
from items, taxes and shipping. please note you have to add an explicit path
when creating your EnhancedPurchase instance.
Example:
from google_measurement_protocol import EnhancedItem, report, EnhancedPurchase
transaction_id = '0001' # any string should do
items = [Item('My awesome product', 90, quantity=2),
Item('Another product', 30))]
transaction = EnhancedPurchase(transaction_id, items, '/cart/')
report('UA-123456-1', user_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:
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')
report('UA-123456-1', user_id, view, extra_info=info, extra_header=headers)