123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- String.prototype.startsWith = function (search) {
- return this.indexOf(search) == 0;
- };
- String.prototype.endsWith = function (search) {
- return original.lastIndexOf(search) == original.length - search.length;
- };
- (function ($) {
-
- var uniqueId = 0;
-
-
- $.fn.analyticsUniqueId = function () {
- if (this.length == 0) {
- return;
- }
- return this.each(function () {
- if (!$(this).attr("id")) {
- $(this).attr("id", "analytics-id-" + ++uniqueId);
- }
- });
- };
- })(jQuery);
- (function ($) {
-
- var settings = {
- attributes: [],
- assignTo: ["a", "input[type='submit']"],
- captureOnce: false,
- client: null,
- exclude: ".analytics-exclude",
- url: null
- };
-
-
-
- function walkTree(element) {
- var tree = [];
- var tagName = $(element).prop("tagName");
-
- if (tagName != undefined) {
- var parent = $(element).parent();
- if (parent != undefined) {
- $.each(walkTree($(element).parent()), function (i, node) {
- tree.push(node);
- });
- }
-
- if (tagName == "HTML" || tagName == "BODY") {
- tree.push(tagName);
- }
- else {
- var tagId = $(element).analyticsUniqueId().attr("id");
- tree.push(tagName + '[id="' + tagId + '"]');
- }
- }
-
- return tree;
- };
-
-
- function identifyPath(node) {
-
- walkTree(node);
- };
-
-
- function initiateTrace(e) {
-
- $this = $(this);
- if (settings.url && !$this.is(".analytics-captured") && !$this.is(settings.exclude)) {
-
-
-
- var data = {
- id: walkTree($this).join(' ')
- };
-
- if (settings.client) {
- data["client"] = settings.client
- }
-
- var dataAttributes = $this.data();
- for (var attribute in dataAttributes) {
- if (attribute.startsWith("analytics")) {
- var cleanName = attribute.replace(/analytics/g, '').toLowerCase();
- data[cleanName] = dataAttributes[attribute];
- }
- }
-
- $.each($(settings.attributes), function (i, attribute) {
- data[attribute] = $this.attr(attribute);
- });
-
- $.ajax({
- type: "POST",
- url: settings.url,
- contentType: "application/x-www-form-urlencoded",
- data: data
- })
- .always(function () {
- if (settings.captureOnce) {
- $this.addClass("analytics-captured");
- }
- });
- }
- };
-
-
-
-
- $.fn.analytics = function (options) {
- if ($(this).length == 0) {
- return;
- }
-
- settings = $.extend({}, settings, options);
-
- var selector = settings.assignTo.join(",");
- return this.each(function () {
-
- $(this).find(selector).andSelf().filter(selector)
- .each(function () {
- identifyPath($(this));
- $(this).on("click", initiateTrace);
- });
- })
- .on("DOMNodeInserted", function (e) {
-
- $(e.target).find(selector).andSelf().filter(selector)
- .each(function () {
- identifyPath($(this));
- $(this).on("click", initiateTrace);
- });
- });
- };
- })(jQuery);
|