How do I apply Google Analytics to the follow button?

codetweaker

I am trying to add google analytics tracking to see how many people click the follow button on a page I am building. I have looked at the developer code below and was wondering do I need to wrap it in some kind of call back function

  1. // First, load the widgets.js file asynchronously
  2. window.twttr = (function (d,s,id) {
  3.   var t, js, fjs = d.getElementsByTagName(s)[0];
  4.   if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
  5.   js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
  6.   return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
  7. }(document, "script", "twitter-wjs"));
  8.  
  9. // Define our custom event handlers
  10. function clickEventToAnalytics (intentEvent) {
  11.   if (!intentEvent) return;
  12.   var label = intentEvent.region;
  13.   pageTracker._trackEvent('twitter_web_intents', intentEvent.type, label);
  14. }
  15.  
  16. function tweetIntentToAnalytics (intentEvent) {
  17.   if (!intentEvent) return;
  18.   var label = "tweet";
  19.   pageTracker._trackEvent('twitter_web_intents', intentEvent.type, label);
  20. }
  21.  
  22. function favIntentToAnalytics (intentEvent) {
  23.   tweetIntentToAnalytics(intentEvent);
  24. }
  25.  
  26. function retweetIntentToAnalytics (intentEvent) {
  27.   if (!intentEvent) return;
  28.   var label = intentEvent.data.source_tweet_id;
  29.   pageTracker._trackEvent('twitter_web_intents', intentEvent.type, label);
  30. }
  31.  
  32. function followIntentToAnalytics (intentEvent) {
  33.   if (!intentEvent) return;
  34.   var label = intentEvent.data.user_id + " (" + intentEvent.data.screen_name + ")";
  35.   pageTracker._trackEvent('twitter_web_intents', intentEvent.type, label);
  36. }
  37.  
  38. // added this code to track follows Probably wrong
  39.  
  40. function followIntentToAnalytics (intentEvent) {
  41.   if (!intentEvent) return;
  42.   var label = intentEvent.data.user_id + " (" + intentEvent.data.screen_name + ")";
  43.   pageTracker._trackSocial('twitter', 'follow', 'myTwitterHandleHere');
  44. }
  45.  
  46.  
  47. // Wait for the asynchronous resources to load
  48. twttr.ready(function (twttr) {
  49.   // Now bind our custom intent events
  50.   twttr.events.bind('click', clickEventToAnalytics);
  51.   twttr.events.bind('tweet', tweetIntentToAnalytics);
  52.   twttr.events.bind('retweet', retweetIntentToAnalytics);
  53.   twttr.events.bind('favorite', favIntentToAnalytics);
  54.   twttr.events.bind('follow', followIntentToAnalytics);
  55. });
47 weeks 2 days ago

Replies

scottPellett
@scottPellett Scott Pellett

Ben,

Were you able to get that code working? I'm trying to do the same thing but I haven't been able to get the intent events to fire.

26 weeks 5 days ago