Skip to content

Commit 65cf24c

Browse files
committed
Merge pull request #2 from bmallred/master
Updated exclusion to work with nested levels
2 parents e7a6743 + 2bf5105 commit 65cf24c

File tree

4 files changed

+72
-56
lines changed

4 files changed

+72
-56
lines changed

example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h1>jQuery Analytics</h1>
1111
<li><a href="#NoMetatdata">No metadata</a></li>
1212
<li><a href="#AssignedId" id="staticId">Assigned identification</a></li>
1313
<li><a href="#DefaultMetadata" data-analytics-dog="terrier">Default metadata given</a></li>
14-
<li><a href="#Excluded" class="analytics-exclude">Excluded link</a></li>
14+
<li class="analytics-exclude"><a href="#Excluded1">Excluded link 1</a> and <a href="#Excluded2">excluded link 2</a></li>
1515
</ul>
1616

1717
<p>These links have been dynamically created at runtime but still are being traced.</p>

jquery-analytics.js

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,23 @@ String.prototype.endsWith = function (search) {
6060
var parent = $(element).parent();
6161
if (parent != undefined) {
6262
$.each(walkTree($(element).parent()), function (i, node) {
63-
tree.push(node);
63+
if ($(node).is(settings.exclude)) {
64+
tree = null;
65+
}
66+
else if (tree !== null) {
67+
tree.push(node);
68+
}
6469
});
6570
}
6671

67-
if (tagName == "HTML" || tagName == "BODY") {
68-
tree.push(tagName);
69-
}
70-
else {
71-
var tagId = $(element).analyticsUniqueId().attr("id");
72-
tree.push(tagName + '[id="' + tagId + '"]');
72+
if (tree !== null) {
73+
if (tagName == "HTML" || tagName == "BODY") {
74+
tree.push(tagName);
75+
}
76+
else {
77+
var tagId = $(element).analyticsUniqueId().attr("id");
78+
tree.push(tagName + '[id="' + tagId + '"]');
79+
}
7380
}
7481
}
7582

@@ -90,60 +97,68 @@ String.prototype.endsWith = function (search) {
9097
$this = $(this);
9198

9299
if (settings.url && !$this.is(".analytics-captured") && !$this.is(settings.exclude)) {
93-
// We prevent the default action to allow the background call to succeed.
94-
var preventedHref = null;
95-
if ($this.attr("href")) {
96-
e.preventDefault();
97-
preventedHref = $this.attr("href");
98-
}
99-
100-
// Initialize the data to be collected.
101-
var data = {};
102-
103-
// Attach the object identifier.
104-
var tree = walkTree($this).join(' ');
105-
if (settings.id) {
106-
data[settings.id] = tree;
107-
}
108-
else {
109-
data["id"] = tree;
110-
}
100+
// Walk the tree.
101+
var tree = walkTree($this);
102+
103+
// Make sure the tree does not include an excluded section.
104+
if (tree !== null && tree.length > 0) {
105+
// Join all the nodes of the tree.
106+
tree = tree.join(' ');
107+
108+
// We prevent the default action to allow the background call to succeed.
109+
var preventedHref = null;
110+
if ($this.attr("href")) {
111+
e.preventDefault();
112+
preventedHref = $this.attr("href");
113+
}
111114

112-
// Attach the client identifier if found.
113-
if (settings.client) {
114-
data["client"] = settings.client
115-
}
115+
// Initialize the data to be collected.
116+
var data = {};
116117

117-
// Assign any "data-analytics-" attributes.
118-
var dataAttributes = $this.data();
119-
for (var attribute in dataAttributes) {
120-
if (attribute.startsWith("analytics")) {
121-
var cleanName = attribute.replace(/analytics/g, '').toLowerCase();
122-
data[cleanName] = dataAttributes[attribute];
118+
// Attach the object identifier.
119+
if (settings.id) {
120+
data[settings.id] = tree;
121+
}
122+
else {
123+
data["id"] = tree;
123124
}
124-
}
125-
126-
// Assign the custom attributes requested to be collected.
127-
$.each($(settings.attributes), function (i, attribute) {
128-
data[attribute] = $this.attr(attribute);
129-
});
130125

131-
// Send the analytics.
132-
$.ajax({
133-
type: "POST",
134-
url: settings.url,
135-
contentType: "application/x-www-form-urlencoded",
136-
data: data
137-
})
138-
.always(function () {
139-
if (settings.captureOnce) {
140-
$this.addClass("analytics-captured");
126+
// Attach the client identifier if found.
127+
if (settings.client) {
128+
data["client"] = settings.client
141129
}
142130

143-
if (preventedHref) {
144-
window.location = preventedHref;
131+
// Assign any "data-analytics-" attributes.
132+
var dataAttributes = $this.data();
133+
for (var attribute in dataAttributes) {
134+
if (attribute.startsWith("analytics")) {
135+
var cleanName = attribute.replace(/analytics/g, '').toLowerCase();
136+
data[cleanName] = dataAttributes[attribute];
137+
}
145138
}
146-
});
139+
140+
// Assign the custom attributes requested to be collected.
141+
$.each($(settings.attributes), function (i, attribute) {
142+
data[attribute] = $this.attr(attribute);
143+
});
144+
145+
// Send the analytics.
146+
$.ajax({
147+
type: "POST",
148+
url: settings.url,
149+
contentType: "application/x-www-form-urlencoded",
150+
data: data
151+
})
152+
.always(function () {
153+
if (settings.captureOnce) {
154+
$this.addClass("analytics-captured");
155+
}
156+
157+
if (preventedHref) {
158+
window.location = preventedHref;
159+
}
160+
});
161+
}
147162
}
148163
};
149164

jquery-analytics.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minify.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yui-compressor -o 'jquery-analytics.min.js' jquery-analytics.js

0 commit comments

Comments
 (0)