Universal Analytics Python 3 Client

1.1.1 · abandoned · verified Thu Apr 16

This library provides a Python 3 interface to Google Analytics, supporting the Universal Analytics Measurement Protocol, with an interface loosely modeled after Google's analytics.js. It's a fork of `universal-analytics-python` with added support for Python 3, batch requests, and both synchronous and asynchronous API calls. The current version is 1.1.1, released on April 28, 2021. However, please note that Universal Analytics (UA) has ceased processing new data as of July 1, 2023, and all UA services and APIs, including the ones this library interacts with, will be fully shut down on July 1, 2024.

Common errors

Warnings

Install

Imports

Quickstart

A simple asynchronous example demonstrating how to initialize a Tracker and send a pageview hit. This example includes an important disclaimer regarding the deprecation and upcoming shutdown of Universal Analytics. Users should be aware that this library is no longer functional for new data collection.

import asyncio
from universal_analytics import Tracker, AsyncHTTPRequest

# NOTE: Universal Analytics has been deprecated by Google.
# This code will NOT send new data to Google Analytics after July 1, 2023,
# and the Universal Analytics API will be completely shut down on July 1, 2024.
# Please migrate to Google Analytics 4 (GA4) and use the appropriate GA4 client library.
# This example is for demonstration purposes only for the 'universal-analytics-python3' library.

GA_TRACKING_ID = 'UA-XXXXX-Y' # Replace with your Universal Analytics Tracking ID
CLIENT_ID = 'unique-user-id-123'

async def main():
    print("Attempting to send Universal Analytics hit (note: UA is deprecated).")
    async with AsyncHTTPRequest() as http:
        tracker = Tracker(GA_TRACKING_ID, http, client_id=CLIENT_ID)
        await tracker.send('pageview', path='/home', title='Homepage')
        print(f"Sent pageview hit for client {CLIENT_ID} to {GA_TRACKING_ID}")

if __name__ == '__main__':
    # Only run if a tracking ID is somewhat plausible
    if GA_TRACKING_ID.startswith('UA-'):
        asyncio.run(main())
    else:
        print("Invalid GA_TRACKING_ID format. Please set a valid 'UA-XXXXX-Y' for demonstration.")

view raw JSON →