Testing Google Analytics tracking code

Either you get the requirements from management to add Google Analytics code to track Ecommerce events or you have it, but need to migrate from analytics.js library (GAT) to global site tag (GST) Puppetry may help you to ensure all the events are being sent within the user flows according to the requirements. Including such tests in CI/CD pipeline makes sure that with any upcoming code changes the defined requirements are still met.

Besides Puppetry validates event payloads in accordance with Google specification. So asserting for Google Analytics tracking events will check if the API is used correctly.

By using page.assertGaTracking in Puppetry we can test the following Google Analytics methods:

  • Common

    • Page View

    • Event

    • Social Interaction

    • App / Screen

    • User Timings

    • Exceptions

  • Ecommerce plugin

    • Adding Item

    • Adding Transaction

  • Enhanced Ecommerce plugin

    • Product Impression

    • Product Click

    • Product Details View

    • Addition to Cart

    • Removal from Cart

    • Checkout Process (Payment)

    • Checkout Process Option

    • Transaction

    • Refund

    • Internal Promotions

Let's see it in practice. Imagine we have a web shop that sells T-shirts. When user clicks on Add to cart link we expect a corresponding event to be sent to Google Analytics.

So the test case to trigger the described event can be following:

The assertion for the expected GST API call may look like that:

When user follows the link he or she gets Shopping Cart popup where it's possible to click Checkout link to continue with the checkout process.

Next user gets Checkout popup to select the payment method and clicks Proceed link to continue.

This user behavior we turn into the test case:

Here we assert for the Checkout / begin_checkout event:

We also assert for the Checkout option event:

Asserting for Checkout / checkout_progress omitted to simplify the example.

Eventually user confirms the purchase :

The case we also cover with tests:

Here we assert for purchase event:

As you see the entire funnel is covered with the tests. If any Google Analytics API calls are missing or do not comply the requirements we are going to be warned in the test report:

Last updated