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.
page.assertGaTracking in Puppetry we can test the following Google Analytics methods:
App / Screen
Enhanced Ecommerce plugin
Product Details View
Addition to Cart
Removal from Cart
Checkout Process (Payment)
Checkout Process Option
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: