瀏覽代碼

Added example

bmallred 12 年之前
父節點
當前提交
ad96b2cb6d
共有 4 個文件被更改,包括 113 次插入20 次删除
  1. 1 0
      .gitignore
  2. 21 0
      MIT.license
  3. 44 0
      example.html
  4. 47 20
      jquery-analytics.js

+ 1 - 0
.gitignore

@ -0,0 +1 @@
1
*.sublime-*

+ 21 - 0
MIT.license

@ -0,0 +1,21 @@
1
The MIT License (MIT)
2
3
Copyright (c) <year> <copyright holders>
4
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
of this software and associated documentation files (the "Software"), to deal
7
in the Software without restriction, including without limitation the rights
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the Software is
10
furnished to do so, subject to the following conditions:
11
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
14
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
THE SOFTWARE.

+ 44 - 0
example.html

@ -0,0 +1,44 @@
1
<html>
2
<head>
3
4
</head>
5
<body>
6
	<h1>jQuery Analytics</h1>
7
8
	<p>The following links have been added by static means.</p>
9
	<ul class="trace">
10
		<li><a href="#NoMetatdata">No metadata</a></li>
11
		<li><a href="#AssignedId" id="staticId">Assigned identification</a></li>
12
		<li><a href="#DefaultMetadata" data-analytics-dog="terrier">Default metadata given</a></li>
13
	</ul>
14
15
	<p>These links have been dynamically created at runtime but still are being traced.</p>
16
	<ul id="dynamic-list" class="trace"></ul>
17
18
	<div id="dynamic-div" class="trace"></div>
19
20
	<p>And these little pigs go home (I mean they are being ignored).</p>
21
	<ul>
22
		<li><a href="http://google.com">Google</a></li>
23
		<li><a href="http://yahoo.com">Yahoo</a></li>
24
		<li><a href="http://bing.com">Bing</a></li>
25
	</ul>
26
27
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
28
	<script type="text/javascript" src="jquery-analytics.js"></script>
29
	<script type="text/javascript">
30
		$(function () {
31
			$(".trace").analytics({
32
				url: "http://localhost/trace"
33
			});
34
35
			$("#dynamic-list")
36
			.append('<li><a href="#DynamicNoMetatdata">Dynamic link with no metatdata</a></li>')
37
			.append('<li><a href="#DynamicAssignedId" id="dynamicId">Dynamic link with assigned identification</a></li>')
38
39
			$("#dynamic-div")
40
			.append('<a href="#DynamicDefaultMetadata" data-analytics-dog="mutt">Dynamic link with metadata given</a>');
41
		});
42
	</script>
43
</body>
44
</html>

+ 47 - 20
jquery-analytics.js

@ -1,17 +1,17 @@
1 1
// The MIT License (MIT)
2
//
2
3 3
// Copyright (c) <year> <copyright holders>
4
//
4
5 5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6 6
// of this software and associated documentation files (the "Software"), to deal
7 7
// in the Software without restriction, including without limitation the rights
8 8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 9
// copies of the Software, and to permit persons to whom the Software is
10 10
// furnished to do so, subject to the following conditions:
11
//
11
12 12
// The above copyright notice and this permission notice shall be included in
13 13
// all copies or substantial portions of the Software.
14
//
14
15 15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -20,17 +20,26 @@
20 20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 21
// THE SOFTWARE.
22 22
23
// Check to see if a string starts with the given search criteria.
24
// @param {String} search
25
// @return {Boolean} a value indicating whether the string starts with the search criteria
23 26
String.prototype.startsWith = function (search) {
24 27
    return this.indexOf(search) == 0;
25
}
28
};
26 29
30
// Check to see if a string ends with the given search criteria.
31
// @param {String} search
32
// @return {Boolean} a value indicating whether the string ends with the search criteria
27 33
String.prototype.endsWith = function (search) {
28 34
    return original.lastIndexOf(search) == original.length - search.length;
29 35
};
30 36
31 37
(function ($) {
38
    // Declared outside of scope to maintain an accurate count.
32 39
    var uniqueId = 0;
33 40
41
    // Provide a unique identifier to an element if one has not already been assigned.
42
    // @return {Object} modified jQuery objects
34 43
    $.fn.analyticsUniqueId = function () {
35 44
        if (this.length == 0) {
36 45
            return;
@ -45,6 +54,7 @@ String.prototype.endsWith = function (search) {
45 54
})(jQuery);
46 55
47 56
(function ($) {
57
    // Default settings which may be extended upon.
48 58
    var settings = {
49 59
        attributes: [],
50 60
        assignTo: ["a", "input[type='submit']"],
@ -53,6 +63,9 @@ String.prototype.endsWith = function (search) {
53 63
        live: false
54 64
    };
55 65
66
    // Walk the tree of a given node.
67
    // @param {Object} element
68
    // @return {Array} path
56 69
    function walkTree(element) {
57 70
        var tree = [];
58 71
        var tagName = $(element).prop("tagName");
@ -70,20 +83,26 @@ String.prototype.endsWith = function (search) {
70 83
        }
71 84
        
72 85
        return tree;
73
    }
86
    };
74 87
88
    // Identify the path to the node.
89
    // @param {Object} node
75 90
    function identifyPath(node) {
76 91
        // Assign identification to all relevant elements.
77 92
        walkTree(node);
78
    }
93
    };
79 94
95
    // Initiate a trace on click.
96
    // @param {Object} e
80 97
    function initiateTrace(e) {
98
        // Locally scope this variable.
81 99
        $this = $(this);
82 100
83
        if (settings.url && !$this.is(".analytics-passthrough")) {
84
            // We prevent the default action to allow the background call to succeed.
85
            e.preventDefault();
101
        if (settings.url && !$this.is(".analytics-captured")) {
102
            // // We prevent the default action to allow the background call to succeed.
103
            // e.preventDefault();
86 104
105
            // Initialize the data to be collected.
87 106
            var data = {
88 107
                id: walkTree($this).join('.')
89 108
            };
@ -107,6 +126,7 @@ String.prototype.endsWith = function (search) {
107 126
                data[attribute] = $this.attr(attribute);
108 127
            });
109 128
129
            // Send the analytics.
110 130
            $.ajax({
111 131
                type: "POST",
112 132
                url: settings.url,
@ -114,11 +134,14 @@ String.prototype.endsWith = function (search) {
114 134
                data: data
115 135
            })
116 136
            .always(function () {
117
                $this.addClass("analytics-passthrough").click();
137
                $this.addClass("analytics-captured");
118 138
            });
119 139
        }
120
    }
140
    };
121 141
    
142
    // Plug-in function providing easy access to analytics.
143
    // @param {Object} options
144
    // @returns {Object} modified jQuery objects
122 145
    $.fn.analytics = function (options) {
123 146
        if ($(this).length == 0) {
124 147
            return;
@ -127,20 +150,24 @@ String.prototype.endsWith = function (search) {
127 150
        // Configure the default settings.
128 151
        settings = $.extend({}, settings, options);
129 152
153
        // Declare the selector to be used.
154
        var selector = settings.assignTo.join(",");
155
130 156
        return this.each(function () {
131
            var selector = settings.assignTo.join(",");
132
            $this = $(this);
133
        
134
            $this.find(selector)
157
            // Itereate through all elements given on initiation.
158
            $(this).find(selector).andSelf().filter(selector)
135 159
            .each(function () {
136 160
                identifyPath($(this));
137
                $(this).click(initiateTrace);
161
                $(this).on("click", initiateTrace);
138 162
            });
139 163
        })
140 164
        .on("DOMNodeInserted", function (e) {
141
            $target = $(e.target);
142
            identifyPath($target);
143
            $target.click(initiateTrace);
165
            // This will capture any dynamically generated content.
166
            $(e.target).find(selector).andSelf().filter(selector)
167
            .each(function () {
168
                identifyPath($(this));
169
                $(this).on("click", initiateTrace);
170
            });
144 171
        });
145 172
    };
146 173
})(jQuery);