Skip to main content

Universal analytics python library

Project description

Universal Analytics for Python

Build Status image codecov License

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())

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:

  • transaction
  • item
  • screenview
  • exception

Property dictionaries permit the same naming conventions given in the analytics.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 the Measurement Protocol Parameter Reference, and properties/parameters can be passed as named arguments.

Example:

# As python named-arguments
tracker.send("pageview", path="/test", title="Test page")

# As property dictionary
tracker.send("pageview", {"path": "/test", "title": "Test page"})

Server-side experiments:

# Set the experiment ID and variation ID
tracker.set("exp", "$experimentId.$variationId")

# Send a pageview hit to Google Analytics
tracker.send("pageview", path="/test", title="Test page")

License

This code is distributed under the terms of the MIT license.

Changes

A full changelog is maintained in the CHANGELOG file.

Contributing

universal-analytics-python3 is an open source project and contributions are welcome! Check out the Issues page to see if your idea for a contribution has already been mentioned, and feel free to raise an issue or submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

universal-analytics-python3-1.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

universal_analytics_python3-1.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file universal-analytics-python3-1.1.0.tar.gz.

File metadata

  • Download URL: universal-analytics-python3-1.1.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for universal-analytics-python3-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6a91b099f9af3830479db534f24be1b8ef846c1197291187d1aabda47672eda7
MD5 7432a533f4e1f190ec8d32c1f823b2a4
BLAKE2b-256 49cc0842cc3e08f6d41a161c10dd77f544886823a155fc4912e6645eadc5a711

See more details on using hashes here.

File details

Details for the file universal_analytics_python3-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: universal_analytics_python3-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for universal_analytics_python3-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a80b7578bb4d595c66d1b25e61bae8c06b7a29cc2f33648fbd6e7e3a68b599e
MD5 0baf057ce9e4c6f4163566e9bcce5335
BLAKE2b-256 0b1a710e4dfc1d41e2e4fbea05e5ef1b6bdf96996d585ec9239931e04f786484

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page