• Stars
    star
    103
  • Rank 323,769 (Top 7 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Python implementation of Google Analytics Measurement Protocol

google-measurement-protocol

A Python implementation of Google Analytics Measurement Protocol

Transaction handling depends on the prices library.

Generating a client ID

Google strongly encourages using UUID version 4 as unique user handles. It's up to you to generate and persist the ID between actions, just make sure that all actions performed by the same user are reported using the same client ID.

import uuid

client_id = uuid.uuid4()

Reporting a page view

There are two ways to obtain a page view data to send to Google Analytics:

pageview(path[, host_name=None][, title=None][, language=None][, referrer=None])
pageview(location='http://example.com/my-page/?foo=1'[, title=None][, language=None][, referrer=None])

Example:

from google_measurement_protocol import pageview, report

data = pageview(path='/my-page/', title='My Page', referrer='http://example.com/')
report('UA-123456-1', client_id, data)

Reporting an event

Use the event function to obtain data:

event('category', 'action'[, label=None][, value=None])

Example:

from google_measurement_protocol import event, report

data = event('profile', 'user_registered')
report('UA-123456-1', client_id, data)

Reporting a transaction

First create Items to describe the contents of the transaction:

item(name, unit_price[, quantity=None][, item_id=None][, category=None])

Then the transaction itself:

transaction(transaction_id, items, revenue[, tax=None][, shipping=None][, affiliation=None])

Example:

from google_measurement_protocol import item, report, transaction
from prices import Money

transaction_id = '0001'  # any string should do
items = [
    item('My awesome product', Money(10, 'EUR'), quantity=2),
    item('Another product', Money(17, 'EUR'))]
data = transaction(transaction_id, items, Money(37, 'EUR'))
report('UA-123456-1', client_id, data)

Reporting an extended ecommerce purchase

For Extended Ecommerce we have implemented Purchase tracking, please note this will add an event automatically, as required by Google Analytics.

First use enhanced_items to describe the contents of the transaction:

enhanced_item(
    name, unit_price[, quantity=None][, item_id=None][, category=None]
    [, brand=None][, variant=None])

Then the enhanced_purchase itself:

enhanced_purchase(
    transaction_id, items, revenue, url_page[, tax=None][, shipping=None]
    [, host=None][, affiliation=None])

Please note you have to add an explicit path when creating your enhanced_purchase instance.

Example:

from google_measurement_protocol import enhanced_item, enhanced_purchase, report

transaction_id = '0001'  # any string should do
items = [
    enhanced_item('My awesome product', Money(10, 'USD'), quantity=2),
    enhanced_item('Another product', Money(15, 'USD'))]
data = enhanced_purchase(transaction_id, items, Money(27, 'USD'), '/cart/')
report('UA-123456-1', client_id, data)

Reporting extra data

In adition to documented arguments, all functions accept any number of extra named arguments, that are then in the data.

For example. to include language parameter in sent event, you may do this:

from google_measurement_protocol import event, report

data = event('profile', 'user_registered', ul='en-us')
report('UA-123456-1', client_id, data)

report also supports passing extra data, which is then added to every payload sent to API. This example is equal to previous one:

from google_measurement_protocol import event, report

data = event('profile', 'user_registered')
report('UA-123456-1', client_id, data, ul='en-us')

You can also pass extra_headers to report() function to submit additional information. It is passed directly as additional headers to requests library. This is currently the only way to pass User-Agent.

Example:

from google_measurement_protocol import PageView, report, SystemInfo

data = pageview(path='/my-page/', title='My Page', referrer='http://example.com/')
headers = {'user-agent': 'my-user-agent 1.0'}
report('UA-123456-1', client_id, data, extra_header=headers)

More Repositories

1

ariadne

Python library for implementing GraphQL servers using schema-first approach.
Python
2,134
star
2

satchless

E-commerce for Python
Python
784
star
3

prices

Python price handling for humans.
Python
267
star
4

ariadne-codegen

Generate fully typed Python client for any GraphQL API from schema, queries and mutations
Python
203
star
5

django-prices

Django fields for the prices module
Python
157
star
6

google-i18n-address

Google's i18n address data packaged for Python.
Python
130
star
7

ariadne-django

ariadne_django makes integrating ariadne and django together easier.
Python
65
star
8

django-images

A database-driven thumbnailing solution for Django
Python
63
star
9

django-messages

A Django application handling private messages between users.
Python
52
star
10

saleor-app-framework-python

Python Saleor App/Extension boilerplate. Batteries included.
Python
49
star
11

chromedebug

A Chrome remote debugging protocol server for Python
Python
36
star
12

ariadne-graphql-modules

Ariadne package for implementing Ariadne GraphQL schemas using modular approach.
Python
34
star
13

django-prices-openexchangerates

openexchangerates.org support for django-prices
Python
33
star
14

ariadne-website

The code that powers Ariadne website
JavaScript
22
star
15

gql-federation-gateway

Simple gateway for Apollo Federation with JWT
JavaScript
18
star
16

django-offsite-storage

Cloud static and media file storage suitable for app containers
Python
18
star
17

serverless-saleor-app-example

Example implementation of Saleor app using AWS Lambda
Python
17
star
18

django-prices-vatlayer

Vatlayer API support for django-prices
Python
15
star
19

django-voice

A simple user feedback application for your Django project
Python
14
star
20

bestest

Some of the bestestâ„¢ and cleverestestâ„¢ code out there
Python
14
star
21

legacy-views

A legacy fork of Saleor that contains the old storefront and dashboard code
Python
14
star
22

graphql-workshop

A GraphQL workshop for DjangoCon EU 2019
Python
13
star
23

django-bootstrap-mockups

A template mocking tool for Django
Python
11
star
24

overv.io-beta

Private beta feedback
11
star
25

django-ariadne-deprecated

Django bindings for the Ariadne GraphQL library.
11
star
26

ariadne-graphql-proxy

Ariadne toolkit for building GraphQL proxies.
Python
11
star
27

trapecssoid

CSS-based trapezoid blocks
CSS
10
star
28

saleor-demo

Saleor's demo setup
HTML
10
star
29

ariadne-graphql-chat-example

Simple Chat application using Ariadne and GraphQL Subscriptions
JavaScript
4
star
30

subgraph-template-ariadne-fastapi

Python
4
star
31

codeclimate-isort

An isort plugin for codeclimate
Python
3
star
32

opentracing-asgi

ASGI middleware reporting to the OpenTracing API
Python
3
star
33

saleor-product-mockups

Free Saleor product templates
3
star
34

saleor-sdk-python

Python
3
star
35

python-invoicible

Invoicible/CentrumFaktur library that provides pure Python interface to CentrumFaktur API
Python
2
star
36

overv.io-splash

Overv.io public webpage
HTML
1
star
37

graphql-wroclaw-website

GraphQL Wrocław Meetup
JavaScript
1
star
38

saleor-graphql-deprecations

Deprecations tracker for Saleor's GraphQL API
Python
1
star
39

js-invoicible

Invoicible/CentrumFaktur library that provides JavaScript interface to CentrumFaktur API
JavaScript
1
star
40

aws_secrets_cache

Cache AWS Secrets
Python
1
star
41

saleor-algolia-demo

Python
1
star
42

smyth

A versatile tool that enhances your AWS Lambda development experience.
Python
1
star