开发者

Tracking tweets from webpage with Google Analytics

开发者 https://www.devze.com 2023-04-02 02:32 出处:网络
According to Google Analytic\'s documentation on social tracking, I\'m supposed to run this code to notify GA when there\'s been a tweet:

According to Google Analytic's documentation on social tracking, I'm supposed to run this code to notify GA when there's been a tweet:

twttr.events.bind('tweet', function(event) {
  if (event) {
    var targetUrl;
    if (event.target && event.target.nodeName == 'IFRAME') {
      targetUrl = extractParamFromUri(event.target.src, 'url');
    }
    _gaq.push(['_trackSocial', 'twitter', 'tweet', targetUrl]);
  }
});

However, I get an error stating twttr is not defined. How do I get notified of a tweet开发者_运维技巧 from my tweet button on my site so that I can track it?


Verify you have included the correct code for the twitter button?describe here

After that open up the console on firebug and write twttr and hit enter. It should return the twttr Object.

Tracking tweets from webpage with Google Analytics

If not verify that your tracking code is below the twitter include script (the one that actually creates the twttr Object)

HTH


If you are loading the Twitter API asynchronously, you need to wrap the twttr.events functions inside the following:

twttr.ready(function(twttr) {

}); 

This way it will wait for the async resources to finish loading before trying to access them.
You can see more here:

Twitter Documentation


If you wrap a check for the type of the twttr object then that will fix it:

// Twitter API 'Tweet' button tracking
    if (typeof(twttr) != 'undefined') {
        twttr.events.bind('click', function(event) {        
            _gaq.push(['_trackSocial', 'twitter', 'tweet', document.location.href, location.pathname]);

        });     
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消