Browse Source

Changes to account for insertion of nodes after initialization

bmallred 12 years ago
parent
commit
44efe46ce7
1 changed files with 72 additions and 57 deletions
  1. 72 57
      jquery-analytics.js

+ 72 - 57
jquery-analytics.js

@ -20,6 +20,14 @@
20 20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 21
// THE SOFTWARE.
22 22
23
String.prototype.startsWith = function (search) {
24
    return this.indexOf(search) == 0;
25
}
26
27
String.prototype.endsWith = function (search) {
28
    return original.lastIndexOf(search) == original.length - search.length;
29
};
30
23 31
(function ($) {
24 32
    var uniqueId = 0;
25 33
@ -37,6 +45,14 @@
37 45
})(jQuery);
38 46
39 47
(function ($) {
48
    var settings = {
49
        attributes: [],
50
        assignTo: ["a", "input[type='submit']"],
51
        url: null,
52
        client: null,
53
        live: false
54
    };
55
40 56
    function walkTree(element) {
41 57
        var tree = [];
42 58
        var tagName = $(element).prop("tagName");
@ -56,12 +72,51 @@
56 72
        return tree;
57 73
    }
58 74
59
    function startsWith(original, search) {
60
        return original.indexOf(search) == 0;
75
    function identifyPath(node) {
76
        // Assign identification to all relevant elements.
77
        walkTree(node);
61 78
    }
62 79
63
    function endsWith(original, search) {
64
        return original.lastIndexOf(search) == original.length - search.length;
80
    function initiateTrace(e) {
81
        $this = $(this);
82
83
        if (settings.url && !$this.is(".analytics-passthrough")) {
84
            // We prevent the default action to allow the background call to succeed.
85
            e.preventDefault();
86
87
            var data = {
88
                id: walkTree($this).join('.')
89
            };
90
91
            // Attach the client identifier if found.
92
            if (settings.client) {
93
                data["client"] = settings.client
94
            }
95
96
            // Assign any "data-analytics-" attributes.
97
            var dataAttributes = $this.data();
98
            for (var attribute in dataAttributes) {
99
                if (attribute.startsWith("analytics")) {
100
                    var cleanName = attribute.replace(/analytics/g, '').toLowerCase();
101
                    data[cleanName] = dataAttributes[attribute];
102
                }
103
            }
104
105
            // Assign the custom attributes requested to be collected.
106
            $.each($(settings.attributes), function (i, attribute) {
107
                data[attribute] = $this.attr(attribute);
108
            });
109
110
            $.ajax({
111
                type: "POST",
112
                url: settings.url,
113
                contentType: "application/x-www-form-urlencoded",
114
                data: data
115
            })
116
            .always(function () {
117
                $this.addClass("analytics-passthrough").click();
118
            });
119
        }
65 120
    }
66 121
    
67 122
    $.fn.analytics = function (options) {
@ -70,62 +125,22 @@
70 125
        }
71 126
72 127
        // Configure the default settings.
73
        var settings = $.extend({
74
            attributes: [],
75
            assignTo: ["a", "input[type='submit']"],
76
            url: null,
77
            client: null
78
        }, options);
128
        settings = $.extend({}, settings, options);
79 129
80 130
        return this.each(function () {
81
            var $elements = $(this).find(settings.assignTo.join(","));
82
            $elements.each(function () {
83
                // Assign identification to all relevant elements.
84
                walkTree($(this));
85
            })
86
            .click(function (e) {
87
                $this = $(this);
88
89
                if (settings.url) {
90
                    // We prevent the default action to allow the background call to succeed.
91
                    e.preventDefault();
92
93
                    var data = {
94
                        id: walkTree($this).join('.')
95
                    };
96
97
                    // Attach the client identifier if found.
98
                    if (settings.client) {
99
                        data["client"] = settings.client
100
                    }
101
102
                    // Assign any "data-analytics-" attributes.
103
                    var dataAttributes = $this.data();
104
                    for (var attribute in dataAttributes) {
105
                        if (startsWith(attribute, "analytics")) {
106
                            var cleanName = attribute.replace(/analytics/g, '').toLowerCase();
107
                            data[cleanName] = dataAttributes[attribute];
108
                        }
109
                    }
110
111
                    // Assign the custom attributes requested to be collected.
112
                    $.each($(settings.attributes), function (i, attribute) {
113
                        data[attribute] = $this.attr(attribute);
114
                    });
115
116
                    $.ajax({
117
                        type: "POST",
118
                        url: settings.url,
119
                        contentType: "application/x-www-form-urlencoded",
120
                        data: data
121
                    })
122
                    .done(function () {})
123
                    .fail(function () {})
124
                    .always(function () {
125
                        // TODO: Continue with the default action.
126
                    });
127
                }
131
            var selector = settings.assignTo.join(",");
132
            $this = $(this);
133
        
134
            $this.find(selector)
135
            .each(function () {
136
                identifyPath($(this));
137
                $(this).click(initiateTrace);
128 138
            });
139
        })
140
        .on("DOMNodeInserted", function (e) {
141
            $target = $(e.target);
142
            identifyPath($target);
143
            $target.click(initiateTrace);
129 144
        });
130 145
    };
131 146
})(jQuery);