Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track clicks on section page infographic / social graphic download buttons #688

Open
zarino opened this issue Sep 3, 2024 · 1 comment
Assignees
Labels
design This is a front-end thing that doesn’t need a developer

Comments

@zarino
Copy link
Member

zarino commented Sep 3, 2024

We’d like some indication of whether people are using the infographics and social graphics, in the gallery on section pages.

@zarino zarino added the design This is a front-end thing that doesn’t need a developer label Sep 3, 2024
@zarino
Copy link
Member Author

zarino commented Sep 10, 2024

@lucascumsille I’ve just realised we don’t currently track any custom events with Google Analytics in the scorecards site, and the way we do it in the caps site makes heavy use of jQuery (which we don’t use in scorecards) so this ticket won’t be quite as straightforward as I thought.

You’ll probably want to add something like our existing trackEvent function to scoring/…/main.js, just without all the jQuery-specific stuff. Something like this:

var trackEvent = function(eventName, params, callback){
  params = params || {};
  callback = callback || function(){};
  params['event_callback'] = callback;
  setTimeout(callback, 2000);
  gtag('event', eventName, params);
};

(We wrap the gtag('event') call like this to gracefully handle the situation where a user has blocked the Google Analytics JavaScript, or some other network error happens – in situations like that, the user will only have to wait a maximum of 2 seconds before the callback fires anyway.)

Then you can add some click handlers to the download buttons however you like (probably using addEventListener inside a forEachElement loop, which we use elsewhere in scoring/…/main.js), prevent the default page navigation, call your new trackEvent function, and do the page navigation in the callback eg:

forEachElement('.js-social-graphic-download', function(el){
  el.addEventListener('click', function(e){
    e.preventDefault();
    var eventName = "…" // TODO!
    var params = {} // TODO!
    trackEvent(eventName, params, function(){
      window.location.href = e.target.href;
    });
  });
});

You’ll need to work out what data to put into the eventName and params variables. Maybe we’ll want to track not just that a download has taken place, but which section it’s on, and which aspect ratio of image was downloaded (square, rectangular, or infographic PDF)…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design This is a front-end thing that doesn’t need a developer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants