Allow custom options when tracking events #365

Merged
merged 2 commits into from Jan 17, 2017

Projects

None yet

3 participants

@alecgibson
Contributor

Adding the ability to track custom dimensions so that we can define them in the options object, and perform calls like GOVUK.analytics.trackEvent('category', 'action', {dimension29: 'dimensionValue'});

This is required by markups suggested here: alphagov/static#875 (review)

@alecgibson alecgibson Allow tracking of custom dimensions
708a4d5
@fofr
Member
fofr commented Jan 10, 2017 edited

I'd prefer an approach similar to #332. See: https://github.com/colinrotherham/govuk_frontend_toolkit/blob/1541d3026a999f4f0cde1a9afc9e5172b6a76005/javascripts/govuk/analytics/google-analytics-universal-tracker.js#L49-L53

This would allow an options object to be passed onto GA itself and would keep trackEvent and trackPageview consistent.

@alecgibson
Contributor

@fofr Do you mean just passing the options object straight through? Fine by me - is there a reason this wasn't done before? I'd sort of figured it was a sanitisation thing? Or do we just trust ourselves to not pass anything silly in?

@fofr
Member
fofr commented Jan 10, 2017

@alecgibson Yep, straight through.

There was a concept of having analytics providers, so google-analytics-universal-tracker is one such provider. There used to be two – the other was google-analytics-classic-tracker. This pattern allowed us to send the same data to both even though they had different APIs. That's not the case any more.

@alecgibson
Contributor

@fofr Great will do. There's a couple of annoying parameter mismatches (eg label => eventLabel) which changing would presumably break a bunch of other stuff? I'm guess I should just leave these for now?

@alecgibson alecgibson Pass event tracking object straight through to Google Analytics
05ab8bb
@alecgibson alecgibson referenced this pull request in alphagov/static Jan 10, 2017
Merged

Track Related Item clicks #875

@fofr fofr changed the title from Allow tracking of custom dimensions to Allow custom options when tracking events Jan 12, 2017
@fofr
fofr approved these changes Jan 12, 2017 View changes
+ it('tracks custom dimensions', function() {
+ universal.trackEvent('category', 'action', {dimension29: 'Home'})
+ expect(window.ga.calls.mostRecent().args).toEqual(
+ ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', dimension29: 'Home'}]
@fofr
fofr Jan 12, 2017 edited Member

Have you confirmed that this object structure correctly passes the dimension to that event hit in GA?

@alecgibson
alecgibson Jan 12, 2017 Contributor

I've now tested this on dev with a forked Frontend Toolkit gem, and a WIP Static branch that sends custom dimensions. All looks ok.

@fofr
Member
fofr commented Jan 12, 2017

A side effect of this change is that through code inspection the page and transport event features are no longer visible, instead only discoverable via the GA api docs.

@alecgibson
Contributor

@fofr yes that's the trade-off, but if we want to allow anything from the GA API, I don't think it's unreasonable to expect people to follow the docs.

@alecgibson
Contributor

@fofr I've now tested this end-to-end with alphagov/static#875

I've verified that the event turns up in the Integration/Staging account of Google Analytics. Good to merge?

@fofr
fofr approved these changes Jan 17, 2017 View changes
@robinwhittleton robinwhittleton merged commit 9b2ed29 into master Jan 17, 2017

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@robinwhittleton robinwhittleton deleted the allow-custom-dimension-tracking branch Jan 17, 2017
@robinwhittleton robinwhittleton added a commit that referenced this pull request Jan 17, 2017
@robinwhittleton robinwhittleton v5.1.0
- Allow custom options when tracking events (PR #365)
69a76ba
@robinwhittleton robinwhittleton referenced this pull request Jan 17, 2017
Merged

v5.1.0 #372

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment