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

Remove old analytics JavaScript #522

Merged
merged 1 commit into from
Jan 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/assets/javascripts/analytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//= require analytics/events-cookie-handler
//= require analytics/tracking-strategies
//= require analytics/tracking
//= require analytics/print-tracking
//= require analytics/print-intent
Expand Down
87 changes: 0 additions & 87 deletions app/assets/javascripts/analytics/events-cookie-handler.js

This file was deleted.

149 changes: 0 additions & 149 deletions app/assets/javascripts/analytics/tracking-strategies.js

This file was deleted.

145 changes: 7 additions & 138 deletions app/assets/javascripts/analytics/tracking.js
Original file line number Diff line number Diff line change
@@ -1,141 +1,10 @@
(function() {
"use strict";
var root = this;
"use strict";

root.GOVUK = root.GOVUK || {};
var GOVUK = root.GOVUK;
window.GOVUK = window.GOVUK || {};
window._gaq = window._gaq || [];

GOVUK.Analytics = GOVUK.Analytics || {};

root._gaq = root._gaq || [];

GOVUK.sendToAnalytics = function (analyticsData) {
root._gaq.push(analyticsData);
};

GOVUK.Analytics.isTheSameArtefact = function(currentUrl, previousUrl, slugLocation) {
var rootOfArtefact = function(url) {
return url.split("/").slice(0, 4 + slugLocation).join("/");
};

var currentSlug = rootOfArtefact(currentUrl).replace(/#.*$/, '');
var previousSlug = rootOfArtefact(previousUrl).replace(/#.*$/, '');
return currentSlug === previousSlug;
};

GOVUK.Analytics.getSlug = function(url, slugLocation) {
return url.split('/')[3 + slugLocation].split("#")[0].split("?")[0];
};

GOVUK.Analytics.isRootOfArtefact = function(url, slugLocation) {
return url.replace(/\/$/, "").split("/").slice(3 + slugLocation).length === 1;
};

GOVUK.Analytics.isLinkToFragmentInCurrentDocument = function(anchorElement) {
var linksToCurrentDocument = anchorElement.href.split("#")[0] === document.URL.split("#")[0];
var hasFragment = anchorElement.hash !== "";
return linksToCurrentDocument && hasFragment;
}

GOVUK.Analytics.startAnalytics = function () {
var ENTER_KEYCODE = 13;
var success = false;
var prefix = 'none';
var format = GOVUK.Analytics.Format,
trackingStrategy = GOVUK.Analytics.Trackers[format];

/**
* Decide whether we should track an event based on a condition function.
* If the condition function isn't defined then the default condition is used.
*
* @param condition, an optional function that returns a boolean
* @return bool
*/
var shouldTrackEvent = function(condition, defaultValue) {
if (typeof condition === "function") {
return condition();
} else {
return defaultValue;
}
};

var createEvent = function(type, isNonInteraction) {
var slug = GOVUK.Analytics.getSlug(document.URL, trackingStrategy.slugLocation);
return ['_trackEvent', prefix + GOVUK.Analytics.Format, slug, type, 0, isNonInteraction];
};

var handleInternalLink = function () {
if (success) return;
success = true;
var event = createEvent("Success", false);
if (GOVUK.Analytics.isLinkToFragmentInCurrentDocument(this)) {
GOVUK.sendToAnalytics(event);
} else {
GOVUK.Analytics.internalSiteEvents.push(event);
}
};

var trackLinks = function(selection, trackExternal) {
// TODO: refactor this to use jQuery("#content").on("click", "a", fireFunction)
selection.each(function () {
var linkToTrack = $(this);

if (this.hostname === window.location.hostname) {
linkToTrack.click(handleInternalLink);
linkToTrack.keydown(function(e) {
if (e.which === ENTER_KEYCODE) {
handleInternalLink.call(this, e);
}
});
}
});
};

var trackingApi = {
trackSuccessFunc: function(isNonInteraction) {
if (isNonInteraction === undefined) {
isNonInteraction = false;
}
return function() {
trackingApi.trackSuccess(isNonInteraction);
};
},
trackSuccess: function (isNonInteraction) {
if (isNonInteraction === undefined) {
isNonInteraction = false;
}
if (success) return;
success = true;
GOVUK.sendToAnalytics(createEvent("Success", isNonInteraction));
},
trackInternalLinks: function(selection) {
trackLinks(selection, false);
},
trackLinks:function (selection) {
trackLinks(selection, true);
},
trackTimeBasedSuccess:function (time) {
setTimeout(trackingApi.trackSuccessFunc(true), time);
}
};

if (GOVUK.Analytics.Trackers[format] !== undefined) prefix = GOVUK.Analytics.Trackers[format].prefix;
if (typeof trackingStrategy === "function") {
var isTheSameArtefact = GOVUK.Analytics.isTheSameArtefact(document.URL, document.referrer, trackingStrategy.slugLocation);
if (shouldTrackEvent(trackingStrategy.shouldTrackEntry, !isTheSameArtefact)) {
GOVUK.sendToAnalytics(createEvent("Entry", true));
GOVUK.Analytics.entryTokens.assignToken();
}
if (shouldTrackEvent(trackingStrategy.shouldTrackSuccess, !isTheSameArtefact)) {
trackingStrategy(trackingApi);
GOVUK.Analytics.entryTokens.revokeToken();
}
}

GOVUK.Analytics.internalSiteEvents.sendAll();

return trackingApi;
};

$(GOVUK.Analytics.startAnalytics);
}).call(this);
GOVUK.sendToAnalytics = function (analyticsData) {
window._gaq.push(analyticsData);
};
}());
Loading