-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from alphagov/remove-analytics
Remove old analytics JavaScript
- Loading branch information
Showing
6 changed files
with
7 additions
and
947 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
149 changes: 0 additions & 149 deletions
149
app/assets/javascripts/analytics/tracking-strategies.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}()); |
Oops, something went wrong.