Track Related Item clicks #875

Merged
merged 9 commits into from Jan 19, 2017

Projects

None yet

4 participants

@alecgibson
Contributor
alecgibson commented Jan 6, 2017 edited

This change will add tracking for clicks on the "Related Item" links.

Trello card: https://trello.com/c/ybdOyNSu/288-enable-tracking-of-related-links-clicks-across-all-gov-uk-applications

The change is almost a direct read-across of the same change for Breadcrumbs:

Note that we are reusing the "Link Text" Custom Dimension 29 (See: https://gov-uk.atlassian.net/wiki/display/GOVUK/Analytics+on+GOV.UK#AnalyticsonGOV.UK-customDimensionsCustomdimensions&suk=ff808081595cb8c4015963b402330000)

@alecgibson alecgibson closed this Jan 6, 2017
@alecgibson alecgibson reopened this Jan 6, 2017
@alecgibson alecgibson changed the title from Track Related Item clicks to [DO NOT MERGE] Track Related Item clicks Jan 6, 2017
@alecgibson
Contributor

Please review, but don't merge yet - I'm going to run this past @carvil when he's back on Monday.

@nickcolley
Contributor

I'm not that well versed with the Analytics code yet but it seems from first glance that we could add the dimensions to the already existing track-click module and remove the breadcrumb module?

I'll add @fofr as a reviewer :)

@nickcolley nickcolley requested a review from fofr Jan 6, 2017
dimension = element.data('track-dimension'),
trackClick = new GOVUK.Modules.TrackClick();
trackClick.start(element);
- function trackBreadcrumbClick(e) {
+ function onClick(e) {
@fofr
fofr Jan 6, 2017 Member

This method name should be descriptive of what the function does, not the circumstance that it's called in

@fofr
Member
fofr commented Jan 6, 2017

I agree with @nickcolley, I don't see the need for both track-click and track-link-click.
It's essentially, "track-click" with a custom dimension set.

@carvil

Looks good, only a couple of tweaks. Please see below.

- category = element.attr('data-track-category'),
- action = element.attr('data-track-action'),
- label = element.attr('data-track-label');
+ category = element.data('track-category'),
@carvil
carvil Jan 9, 2017 Member

I think we should use attr() instead of data() when fetching data from these data attributes, more details here: ebe69aa#diff-5649f4c1766856649d723e2ef9a026b3

@alecgibson
alecgibson Jan 9, 2017 Contributor

Will do. Although the dimension has always been a number rather than a string - should we keep it that way? (Ie use element.data('track-custom-dimension') where all the others are .attr?

@alecgibson
alecgibson Jan 9, 2017 Contributor

As discussed, custom dimension should remain data because Google Analytics expects an integer.

@@ -14,7 +14,8 @@
track_action: index + 1,
track_label: crumb[:url],
track_dimension: crumb[:title],
- module: 'track-breadcrumb-click'
+ track_custom_dimension: 29,
@carvil
carvil Jan 9, 2017 Member

We could perhaps set this in a static variable with a name that explains when this is in case people wonder what the number 29 is all about. Something like:

LINK_TEXT_CUSTOM_DIMENSION=29

And then use it throughout. What do you think?

@alecgibson
alecgibson Jan 9, 2017 Contributor

Sounds good to me - where should it live?

@alecgibson
alecgibson Jan 9, 2017 Contributor

As discussed, will leave this for now.

+ );
+ });
+
+ it('tracks related item click events', function() {
@carvil
carvil Jan 9, 2017 Member

Could you please also add a test that makes sure we call GOVUK.analytics.trackEvent but don't call GOVUK.analytics.setDimension when the custom dimension attribute isn't present?

@alecgibson
alecgibson Jan 9, 2017 Contributor

Aren't the changes to 'tracks click events' sufficient? (lines 11 & 28). Or did you want it to be more explicit than that?

@alecgibson
alecgibson Jan 9, 2017 Contributor

As discussed, this is fine.

@alecgibson alecgibson changed the title from [DO NOT MERGE] Track Related Item clicks to Track Related Item clicks Jan 9, 2017
@carvil
carvil approved these changes Jan 9, 2017 View changes
+ }
+
+ if (customDimension && GOVUK.analytics.setDimension) {
+ GOVUK.analytics.setDimension(customDimension, dimension);
@nickcolley
nickcolley Jan 9, 2017 Contributor

If dimension is not set, could this break?

@alecgibson
alecgibson Jan 9, 2017 Contributor

I've added a check and some tests for this.

- label = element.attr('data-track-label');
+ label = element.attr('data-track-label'),
+ dimension = element.attr('data-track-dimension'),
+ customDimension = element.data('track-custom-dimension');
@fofr
fofr Jan 10, 2017 Member

Are these two variables the custom dimension index and the custom dimension value?

@alecgibson
alecgibson Jan 10, 2017 Contributor

Yes I believe so - I can rename if you want? (I'm still only just getting my head around everything!)

@fofr
fofr Jan 10, 2017 Member

I think it'd be clearer if renamed

+ }
+
+ if (customDimension && dimension && GOVUK.analytics.setDimension) {
+ GOVUK.analytics.setDimension(customDimension, dimension);
@fofr
fofr Jan 10, 2017 Member

I think this dimension will remain set for all subsequent tracked-clicks.

eg If I cmd+click and open a page in a new tab. Then on the old page if I click an external link or a download – (two types of links that get tracked), those will be tracked with this dimension.

@alecgibson
alecgibson Jan 10, 2017 Contributor

Very possible - I have no idea how this is put together. How do I "unset" it? Set it to undefined?

@alecgibson
alecgibson Jan 10, 2017 Contributor

(I guess technically for compatibility it should be set to null?)

@fofr
fofr Jan 10, 2017 Member

Rather than setting the dimension I think we should pass it as an option into the event itself:

See: https://github.com/alphagov/govuk_frontend_toolkit/blob/c4acf649030d8a7be1e1533117d36f9bebbe038b/javascripts/govuk/analytics/analytics.js#L37-L45

And:
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets

eg

GOVUK.analytics.trackEvent(category, action, {dimension29: "dimensionValue"});

That should work.

@alecgibson
alecgibson Jan 10, 2017 Contributor

As a side-note, I'm not sure we do track downloads? For example the PDF on the Computing Curriculum page doesn't have any tracking markup, and I don't see any events fired off to GA in my Network tab. Unless it's a specific type of download?

@alecgibson
alecgibson Jan 10, 2017 Contributor

Similarly, I don't see any tracking markup on the link to the DHS Website on this page? Are you aware of an actual case I can test?

@alecgibson
alecgibson Jan 10, 2017 Contributor

(The above solution looks good to me, but I'd like to confirm on a test case if you can find one?)

@fofr
fofr Jan 10, 2017 edited Member

That's configured here:
https://github.com/alphagov/static/blob/9e8795b262c948a65b798029864cbe04f193262a/app/assets/javascripts/analytics/static-analytics.js#L26-L28

And here:
https://github.com/alphagov/govuk_frontend_toolkit/blob/master/javascripts/govuk/analytics/download-link-tracker.js

Though you're right, I can't see anything being tracked.
I see the requests in my network tab correctly (my console had filtered to show only img requests, switching to all showed the correct events being sent through in a request payload)

@alecgibson alecgibson changed the title from Track Related Item clicks to [DO NOT MERGE] Track Related Item clicks Jan 10, 2017
@alecgibson
Contributor

This pull request is now dependent upon the deployment of alphagov/govuk_frontend_toolkit#365

@alecgibson alecgibson changed the title from [DO NOT MERGE] Track Related Item clicks to Track Related Item clicks Jan 18, 2017
@alecgibson
Contributor

@fofr @nickcolley this should now be good to go. It was waiting on the Frontend Toolkit Gem getting bumped (which it now has). I've tested this change end-to-end with my changes to the Frontend Toolkit Gem.

@carvil
Member
carvil commented Jan 18, 2017

I've tried it in integration and I can see the GA events being fired correctly. 👍

@fofr
fofr approved these changes Jan 18, 2017 View changes
@alecgibson alecgibson merged commit b4a15d6 into master Jan 19, 2017

1 check passed

default "Build #1434 succeeded on Jenkins"
Details
@alecgibson alecgibson deleted the nav-288-track-related-item-clicks branch Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment