Skip to main content

Testing Google Analytics implementations within CI pipelines

Project description

GAUnit

Build Status Documentation Status

GAUnit is a Python library for testing Google Analytics implementations with Selenium or RobotFramework test cases.

It is designed to be used within your pipelines in various environments such as traditional websites, Single Page Applications or Mobile Apps.

Installation

You will need Python 3.7+ installed.

pip install gaunit

Usage

Define your expected GA tracking plan for a given test case. Example : tracking the "play" button on a video from a product page:

{
    "my_test_case": [
        {
            "t": "pageview",
            "dp": "my_product_page_name"
        },
        {
            "t": "event",
            "ec": "Video",
            "ea": "Play"
        }]
}

Run your automated test wih Python

Run a test, export har and check it against your expected tracking plan:

import gaunit
import browsermobproxy

# Run your Selenium test here and export har 
# (see samples below or how-tos for more details)
# ...

# import tracking plan from json file or event from Google Sheets
tracking_plan = gaunit.TrackingPlan.from_json("tracking_plan.json")
# check GA events
result = gaunit.check_har(
    "my_test_case", tracking_plan, har_path="my_test_case.har"
)
print(result.checklist_expected)
# [True, True] congrats! both events (pageview and click) were  fired.

Or manually check HAR files with command line

Alternatively to automatic tests, you can manually browse your website, export a HAR file and check it through command line :

$ gaunit test_case.har my_test_case  # passed
events in tracking plan: 3
--------------------------------------------------------------------------------
GA events found: total:4 / ok:3 / missing:0
✔ OK: all expected events found

$ gaunit test_case.har my_test_case  # failed
events in tracking plan: 3
================================================================================
{'t': 'event', 'ec': 'Video', 'ea': 'Play'}
                                                                     ... missing
--------------------------------------------------------------------------------
GA events found: total:11 / ok:1 / missing:2
❌ FAILED: events missing

Run your first tests (full working samples)

Automated test using a proxy

We can use BrowserMob Proxy to intercept Google Analytics events.

  • Install Selenium and Browsermob Proxy packages:

    pip install selenium browsermob-proxy
    
  • Download BrowserMob Proxy latest release (note: install Java).

    • add bin/ directory to your %PATH
  • Download ChromeDriver

    • add it to your %PATH or copy it in your working directory (more details here)
  • Run the test:

    python samples/auto_test_with_proxy/demo_store_add_to_cart.py
    

Automated test with Performance Log (Chrome only)

Performance Log is a fast and easy way to intercept GA events (GET events only). It works without BrowserMob Proxy.

Note: this method works for analytics.js but might not work for new GA implementations, such as transport beacon or GA4.

  • Install Selenium and ChromeDriver (same as above)

  • Run the test:

    python samples/auto_test_with_perf_log/demo_store_add_to_cart.py
    

Robot Framework

If you want to use RobotFramework, check GAUnit Library for Robot Framework

Documentation

Still work in progress, sorry!

Full documentation will soon be available here.

In the meantime, please refer to samples and Developer interface.

Why GAUnit?

Testing your Google Analytics implementation is often time consuming and, let's say it, sometimes very boring!

But most of all, if your tracking is not reliable as your application evolves, your reportings won't be either. People in your company will loose confidence in your reportings when they have to take important business decisions. You will provide great reportings if you integrate tracking in your DevOps pipelines (and thus, in you Quality Assurance plan).

Some great tools let you automatically test your DataLayer, but sometimes it is not enough: you not only want to test pageviews, but also events like clicks and Ecommerce. You might want to test tracking in various environments like Single Page Application, AMP or Mobile Applications. GAUnit lets you do just that.

Contributing

GAUnit can be useful for several companies. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Licence

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

GAUnit was inspired by WAUnit. We decided to create a new library commpatible with Python 3 and easier to set up.

Roadmap

  • Complete test case samples with RobotFramework mobile app
  • Tracking plan using analytics.js, GTM or GA4 syntax
  • Dockerize (for simpler set up)

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

gaunit-0.3.2.tar.gz (14.2 kB view hashes)

Uploaded Source

Supported by

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