Server side implementation of Google Analytics in Python.


Keywords
google, analytics, server, side, python
License
GPL-3.0
Install
pip install pygass==0.3.20

Documentation

Python google analytics library

Introduction

This library is a simple server side implementation of google analytics it is based on the documentation here. https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

It uses the requests library to send requests directly to google with out any javascript which may be blocked client side.

Installation

pip install pygass

The code is also available at this location for bug fixes or to pull from master. https://gitlab.com/python-open-source-library-collection/pygass

Development

When testing its usefull to use the analytics hit builder to check the correct format. https://ga-dev-tools.appspot.com/hit-builder/

Examples

Simple Page tracking event

The below example sends a page view event to google, you will need to provide an anonymous client id and the page url as a minimum.

import random
import pprint
import constants as st
import pygass as an

# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_pageview(client_id=123, page="/test/client/pageview"), width=1
)

The below example demonstrates how to track a transaction with analytics, you will need to send an anonymous client id with the requests.

Simple transaction tracking event

import random
import pprint
import constants as st
import pygass as an

# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
transaction_id = random.randint(1, 10000000)
an.track_transaction(client_id=1001, transaction_id=transaction_id)
pprint.pprint(
    an.track_item(
        client_id=1001, transaction_id=transaction_id, name="Test Product 3"
    ),
    width=1,
)

Simple enhanced ecommerce impression

import random
import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an

# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
transaction_id = random.randint(1, 10000000)
pprint.pprint(
    an.track_enhanced_ecommerce_impression(
        client_id=1001, product_id=1001, product_name="Test Product 3", page="/test/client/pageview"
    ),
    width=1,
)

Simple enhanced ecommerce action

import random
import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an

# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_enhanced_ecommerce_action(
        client_id=1001,
        category="cat01",
        action="view",
        product_id=1337,
        product_name="Test Product 3",
        product_action="detail",
        product_category= "Product Test Category",
        page="/test/page"),
    width=1,
)

Simple enhanced ecommerce view product

import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an
# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"
# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_enhanced_ecommerce_impression(
        client_id=1001,
        category="cat01",
        action="click",
        product_action="add",
        product_id=1001,
        product_name="Test Product 3",
        product_category="Product Test Category",
        page="/test/page"),
    width=1,
)

v=1&t=pageview&tid=UA-10000000-1&cid=555&dh=mydemo.com&dp=%2Fhome&dt=homepage

&il1nm=Search%20Results&il1pi1id=P12345&il1pi1nm=Android%20Warhol%20T-Shirt&il1pi1ca=Apparel%2FT-Shirts&il1pi1br=Google&il1pi1va=Black&il1pi1ps=1&il1pi1cd1=Member&il2nm=Recommended%20Products&il2pi1nm=Yellow%20T-Shirt&il2pi2nm=Red%20T-Shirt

Simple enhanced ecommerce product add to basket

import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an
# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_enhanced_ecommerce_add_to_basket(
        client_id=1001,
        category="cat01",
        action="click",
        transaction_id=20,
        product_id=1001,
        product_name="Test Product 3",
        product_category="Product Test Category",
        page="/test/page"),
    width=1,
)

Simple enhanced ecommerce product purchase checkout

import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an
# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_enhanced_ecommerce_checkout(
        client_id=1001,
        category="cat01",
        action="click",
        transaction_id=20,
        product_id=1001,
        product_name="Test Product 3",
        product_category="Product Test Category",
        product_action="purchase",
        page="/test/page"),
    width=1,
)

import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an
# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_enhanced_ecommerce_add_to_cart(
        client_id=1001,
        category="cat01",
        action="click",
        transaction_id=21,
        product_id=1001,
        product_name="Test Product 3",
        product_category="Product Test Category",
        product_action="add",
        page="/test/page"),
    width=1,
)

Simple enhanced ecommerce product purchase transaction

import pprint
import pygass.constants as st
import pygass.enhanced_ecommerce as an
# set your analytics code
st.ANALYTICS_CODE = "UA-10000000-1"

# use the test api for json responses
st.ANALYTICS_URL = "https://www.google-analytics.com/debug/collect"

# show the response
pprint.pprint(
    an.track_enhanced_ecommerce_purchase(
        client_id=1001,
        category="cat01",
        action="click",
        transaction_id=20,
        product_id=1001,
        product_name="Test Product 3",
        product_category="Product Test Category",
        product_action="purchase",
        affiliation= "Test Merchant",
        revenue= "0.0",
        page="/test/page"),
    width=1,
)