Package: universal-analytics-python3
Universal Analytics for PythonIt's a fork ofuniversal-analytics-pythonwhith 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'sanalytics.js.NOTEthis project is reasonably feature-complete for most use-cases, covering
all relevant features of the Measurement Protocol, however we still consider itbeta. Please feel free to file issues for feature requests.InstallationThe easiest way to install universal-analytics is directly from PyPi usingpipby running the following command:pipinstalluniversal-analytics-python3UsageFor 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:fromuniversal_analyticsimportTracker,HTTPRequest,HTTPBatchRequestwithHTTPRequest()ashttp:tracker=Tracker("UA-XXXXX-Y",http,client_id="unique-id")tracker.send("event","Subscription","billing")withHTTPBatchRequest()ashttp:tracker=Tracker("UA-XXXXX-Y",http,client_id="unique-id")tracker.send("event","Subscription","billing")A simple example for asynchronous usage:importasynciofromuniversal_analyticsimportTracker,AsyncHTTPRequest,AsyncHTTPBatchRequestasyncdefmain():asyncwithAsyncHTTPRequest()ashttp:tracker=Tracker("UA-XXXXX-Y",http,client_id="unique-id")awaittracker.send("event","Subscription","billing")asyncwithAsyncHTTPBatchRequest()ashttp:tracker=Tracker("UA-XXXXX-Y",http,client_id="unique-id")awaittracker.send("event","Subscription","billing")loop=asyncio.get_event_loop()loop.run_until_complete(main())This library support the following tracking types, with corresponding (optional) arguments:pageview: [ page path ]event: category, action, [ label [, value ] ]social: network, action [, target ]timing: category, variable, time [, label ]Additional tracking types supported with property dictionaries:transactionitemscreenviewexceptionProperty dictionaries permit the same naming conventions given in theanalytics.js Field Reference,
with the addition of common spelling variations, abbreviations, and hyphenated names
(rather than camel-case).Further, the property dictionaries support names as per theMeasurement Protocol Parameter Reference,
and properties/parameters can be passed as named arguments.Example:# As python named-argumentstracker.send("pageview",path="/test",title="Test page")# As property dictionarytracker.send("pageview",{"path":"/test","title":"Test page"})Server-side experiments:# Set the experiment ID and variation IDtracker.set("exp","$experimentId.$variationId")# Send a pageview hit to Google Analyticstracker.send("pageview",path="/test",title="Test page")LicenseThis code is distributed under the terms of the MIT license.ChangesA full changelog is maintained in theCHANGELOGfile.Contributinguniversal-analytics-python3is an open source project and contributions are
welcome! Check out theIssuespage to see if your idea for a contribution has already been mentioned, and feel
free to raise an issue or submit a pull request.