Universal analytics python library
pip install universal-analytics-python3Universal Analytics for Python
It's a fork of universal-analytics-python whith support for Python 3, batch requests, synchronous and asynchronous API calls.
This library provides a Python interface to Google Analytics, supporting the
Universal Analytics Measurement Protocol, with an interface modeled (loosely)
after Google's analytics.js.
NOTE this project is reasonably feature-complete for most use-cases, covering all relevant features of the Measurement Protocol, however we still consider it beta. Please feel free to file issues for feature requests.
Installation
The easiest way to install universal-analytics is directly from PyPi using pip
by running the following command:
pip install universal-analytics-python3
Usage
For the most accurate data in your reports, Analytics Pros recommends establishing a distinct ID for each of your users, and integrating that ID on your front-end web tracking, as well as back-end tracking calls. This provides for a consistent, correct representation of user engagement, without skewing overall visit metrics (and others).
A simple example for synchronous usage:
from universal_analytics import Tracker, HTTPRequest, HTTPBatchRequest
with HTTPRequest() as http:
tracker = Tracker("UA-XXXXX-Y", http, client_id="unique-id")
tracker.send("event", "Subscription", "billing")
with HTTPBatchRequest() as http:
tracker = Tracker("UA-XXXXX-Y", http, client_id="unique-id")
tracker.send("event", "Subscription", "billing")
A simple example for asynchronous usage:
import asyncio
from universal_analytics import Tracker, AsyncHTTPRequest, AsyncHTTPBatchRequest
async def main():
async with AsyncHTTPRequest() as http:
tracker = Tracker("UA-XXXXX-Y", http, client_id="unique-id")
await tracker.send("event", "Subscription", "billing")
async with AsyncHTTPBatchRequest() as http:
tracker = Tracker("UA-XXXXX-Y", http, client_id="unique-id")
await tracker.send("event", "Subscription", "billing")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())