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.
page.assertGaTrackingin Puppetry we can test the following Google Analytics methods:
- Page View
- Social Interaction
- App / Screen
- User Timings
- 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
- 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:
Test case example for adding products to the shopping cart
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.
Starting the checkout process
Next user gets Checkout popup to select the payment method and clicks Proceed link to continue.
Checkout process step
This user behavior we turn into the test case:
Testing checkout progress
Asserting checkout progress event
Asserting checkout step event
Asserting for Checkout / checkout_progress omitted to simplify the example.
Eventually user confirms the purchase :
The case we also cover with tests:
Testing 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:
Assertion for Google Analytics event failed