To validate tracking of Enhanced E-Commerce actions, use the following Array in your Jasmine expectations:
['ec:addProduct', { ─────────────────────> 1. (Standard Google Analytics parameters)
'id': 'P12345', ──────────────────────> 2. Product ID
'name': 'Android Warhol T-Shirt', ────> 3. Product Name
'category': 'Apparel', ───────────────> 4. Product Category
'brand': 'Google', ───────────────────> 5. Product Brand
'variant': 'black', ──────────────────> 6. Product Variant
'price': '29.20', ────────────────────> 7. Product Price
'quantity': 1 ──────────────────────> 8. Product quantity
}]
['ec:setAction', 'checkout', { ──────────> 1&2. (Standard Google Analytics parameters)
'step': 1, ───────────────────────────> 3. Checkout Step
'option': 'MasterCard' ───────────────> 4. Checkout Option
}]
Using direct ga()
calls, checking proper tracking of Enhanced E-Commerce actions could be done in the following manner:
describe('The Google Analytics "Enhanced E-Commerce" tracking', function () {
beforeEach(function () {
// Load the page to test:
browser.get('index.html');
// Register the Google Analytics Event Data Interceptor:
browser.driver.registerGoogleAnalyticsEventDataInterceptor();
});
it('should fire Enhanced E-Commerce events when proceeding to the Step #1 of the Checkout process', function (done) {
// Get the "EventBuffer" object back from the browser:
browser.driver.executeScript(function () {
return window.GAWebTester.getEventBuffer();
})
.then(
// Validate the content of the "EventBuffer" object:
function successCallback (EventBuffer) {
expect( EventBuffer ).toContain( ['ec:ec:addProduct', {
'id': 'P12345',
'name': 'Android Warhol T-Shirt',
'category': 'Apparel',
'brand': 'Google',
'variant': 'black',
'price': '29.20',
'quantity': 1
}] );
expect( EventBuffer ).toContain( ['ec:setAction', 'checkout', {
'step': 1,
'option': 'MasterCard'
}] );
},
// If there was an error getting back the "EventBuffer" object from the browser, fail the test:
function errorCallback (error) {
fail('Should not have received Error: ' + JSON.stringify(error));
}
)
.then(done);
});
});