Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #28 from groovecoder/hacks-analytics-27
Browse files Browse the repository at this point in the history
fix #27 - outbound link tracking in analytics
  • Loading branch information
darkwing committed Apr 22, 2014
2 parents 28615ed + 840e8df commit d16502e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 14 deletions.
9 changes: 3 additions & 6 deletions themes/Hacks2013/functions.php
Expand Up @@ -237,12 +237,9 @@ function mozhacks_load_scripts() {
wp_enqueue_script( 'socialshare' );
}

// Register and load socialshare tracking script with jquery & socialshare
// dependencies
wp_register_script( 'socialtrack', get_template_directory_uri() . '/js/socialtrack.js', array('jquery', 'socialshare'));
if ( is_singular() && get_option('mozhacks_share_posts') && !is_page(array('home','about','demos','articles')) ) {
wp_enqueue_script( 'socialtrack', get_template_directory_uri() . '/js/socialtrack.js' );
}
// Register and load analytics script with jquery & socialshare dependencies
wp_register_script( 'analytics', get_template_directory_uri() . '/js/analytics.js', array('jquery', 'socialshare'));
wp_enqueue_script( 'analytics', get_template_directory_uri() . '/js/analytics.js' );
}
add_action( 'wp_enqueue_scripts', 'mozhacks_load_scripts' );

Expand Down
1 change: 1 addition & 0 deletions themes/Hacks2013/header.php
Expand Up @@ -53,6 +53,7 @@
<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

<script type="text/javascript">
window.hacks = {};
// http://cfsimplicity.com/61/removing-analytics-clutter-from-campaign-urls
var removeUtms = function(){
var l = window.location;
Expand Down
120 changes: 120 additions & 0 deletions themes/Hacks2013/js/analytics.js
@@ -0,0 +1,120 @@
window.hacks = window.hacks || {};
(function(win, doc, $) {
'use strict';

// Adding to globally available hacks object
var analytics = hacks.analytics = {
/*
Tracks generic events passed to the method
*/
trackEvent: function(eventArray, callback) {
// Submit eventArray to GA and call callback only after tracking has
// been sent, or if sending fails.
//
// callback is optional.
//
// Example usage:
//
// $(function() {
// var handler = function(e) {
// var self = this;
// e.preventDefault();
// $(self).off('submit', handler);
// gaTrack(
// ['Newsletter Registration', 'submit'],
// function() { $(self).submit(); }
// );
// };
// $(thing).on('submit', handler);
// });

var _gaq = win._gaq;
var timeout;
var timedCallback;

// Create the final event array
eventArray = ['_trackEvent'].concat(eventArray);

// Create the timed callback if a callback function has been provided
if (typeof(callback) == 'function') {
timedCallback = function() {
clearTimeout(timeout);
callback();
};
}

// If Analytics has loaded, go ahead with tracking
if (_gaq && _gaq.push) {
// Send event to GA
_gaq.push(eventArray);
// Only set up timeout and hitCallback if a callback exists.
if (timedCallback) {
// Failsafe - be sure we do the callback in a half-second
// even if GA isn't able to send in our trackEvent.
timeout = setTimeout(timedCallback, 500);

// But ordinarily, we get GA to call us back immediately after
// it finishes sending our things.
// https://developers.google.com/analytics/devguides/collection/gajs/#PushingFunctions
// This is called after GA has sent the current pending data:
_gaq.push(timedCallback);
}
}
else if(callback) {
// GA disabled or blocked or something, make sure we still
// call the caller's callback:
callback();
}
},

/*
Track all outgoing links
*/
trackOutboundLinks: function(target) {
$(target).on('click', 'a', function (e) {
// If we explicitly say not to track something, don't
if($(this).hasClass('no-track')) {
return;
}

var host = this.hostname;

if(host && host != location.hostname) {
var newTab = (this.target == '_blank' || e.metaKey || e.ctrlKey);
var href = this.href;
var callback = function() {
location = href;
};
var data = ['Outbound Links', href];

if (newTab) {
analytics.trackEvent(data);
} else {
e.preventDefault();
analytics.trackEvent(data, callback);
}
}
});
},

/*
Track all socialshare clicks
*/
trackSocialShareClicks: function() {
$('.share').click(function(){
if ($(this).find('.socialshare').hasClass('open')) {
analytics.trackEvent(['socialshare', 'close']);
} else {
analytics.trackEvent(['socialshare', 'open']);
}
});
}
};

$(doc).ready(function(){
analytics.trackSocialShareClicks();
analytics.trackOutboundLinks(doc.body);
});

})(window, document, jQuery);

8 changes: 0 additions & 8 deletions themes/Hacks2013/js/socialtrack.js

This file was deleted.

0 comments on commit d16502e

Please sign in to comment.