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

option hitCallback doesn't work for google analytics. #102

Open
devinrhode2 opened this issue Oct 15, 2016 · 3 comments
Open

option hitCallback doesn't work for google analytics. #102

devinrhode2 opened this issue Oct 15, 2016 · 3 comments

Comments

@devinrhode2
Copy link

devinrhode2 commented Oct 15, 2016

I'm trying to track outbound link navigations, and found that the way to do this with google analytics is to send an event and in the options set a hitCallback key to a function that will be called when the event has been received. This just flat out doesn't seem to work.. here's my component.js:

function exitToMessenger() {
  console.log('redirecting to messenger.com!!')
  window.location.href = 'https://www.messenger.com/t/heyimlea';
}

export default Ember.Component.extend({
  metrics: Ember.inject.service(),
  actions: {
    sendToMessenger() {
      console.log('send to messenger called')
      var metrics = Ember.get(this, 'metrics')

      var eventData = {
        hitType: 'event',
        eventCategory: 'Outbound Link',
        eventAction: 'click',
        eventLabel: 'Sent to messenger!',
        eventValue: 'https://www.messenger.com/t/heyimlea'
      };

      // if (navigator.sendBeacon) {
      //   //https://github.com/googleanalytics/autotrack/blob/bcdd409c22430553a2a43275386877bf37600e68/lib/plugins/outbound-form-tracker.js#L77
      //   //https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
      //   eventData.transport = 'beacon'
      //   metrics.trackEvent(eventData)
      //   //metrics.trackPage({page: '/sent2messenger', title: "exiting to messenger" });
      //   //be sure analytics.js calls sendBeacon before exiting
      //   setTimeout(exitToMessenger, 4)
      // } else {
        eventData.hitCallback = exitToMessenger;
        metrics.trackEvent(eventData)
      // }
    }
  }

Searching something along the lines of 'analytics track outbound link' I find this help article from google analytics suggesting this method: https://support.google.com/analytics/answer/1136920?hl=en
Here's the docs on hitCallback: https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#hitcallback

I'd love to get this fixed and tested asap so that the ember community can have one comprehensive addon for analytics

@devinrhode2
Copy link
Author

Even using the trackPage function doesn't work:

        metrics.trackPage({
          page: '/sent2messenger',
          title: 'sent2messenger',
          hitCallback: exitToMessenger
        })

@devinrhode2
Copy link
Author

devinrhode2 commented Oct 15, 2016

Also I think it'd be good to take whatever my solution ends up being and add it to an #Extra Examples section around the bottom of the readme (figuring out the eventCategory, eventAction, etc keys was a little tricky)

@devinrhode2
Copy link
Author

fix pending merge here: #103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant