Skip to content

Commit

Permalink
Merge pull request googleanalytics#44 from googleanalytics/outbound-l…
Browse files Browse the repository at this point in the history
…ink-tracker

Ensure only http(s) links are considered outbound
  • Loading branch information
philipwalton committed Mar 30, 2016
2 parents a92959c + e97da2b commit 5acf843
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ ga('require', 'autotrack', {
```js
function(link) {
return link.hostname != location.hostname;
return link.hostname != location.hostname &&
link.protocol.indexOf('http') === 0;
};
```
Expand All @@ -377,11 +378,14 @@ The default `shouldTrackOutboundLink` option will consider a link click from `bl
ga('require', 'autotrack', {
shouldTrackOutboundLink: function(link) {
// Checks that the link's hostname does not contain "example.com".
return link.hostname.indexOf('example.com') < 0;
return link.hostname.indexOf('example.com') < 0 &&
link.protocol.indexOf('http') === 0;
}
}
```
The default `shouldTrackOutboundLink` option also only tracks links with the `http:` or `https:` protocols. You can remove this check if you'd like to track protocols like `tel:` or `mailto:` as outbound links.
### `shouldTrackUrlChange`
**Type**: `Function`
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/outbound-link-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ OutboundLinkTracker.prototype.handleLinkClicks = function(event) {
* @return {boolean} Whether or not the link should be tracked.
*/
OutboundLinkTracker.prototype.shouldTrackOutboundLink = function(link) {
return link.hostname != location.hostname;
return link.hostname != location.hostname &&
link.protocol.indexOf('http') === 0;
};


Expand Down
2 changes: 2 additions & 0 deletions test/outbound-link-tracker.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<a id="outbound-link" href="http://google-analytics.com/collect">Outbound</a>
<a id="local-link" href="/test/blank.html">Local</a>
<a id="javascript-protocol" href="javascript:console.log('clicked!')">javascript protocol</a>
<a id="file-protocol" href="file:///content.txt">file protocol</a>

</body>
</html>
Expand Down
20 changes: 18 additions & 2 deletions test/outbound-link-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('outboundLinkTracker', function() {
.execute(ga.getHitData))
.value;

assert.equal(hitData.length, 1);
assert.equal(hitData[0].eventCategory, 'Outbound Link');
assert.equal(hitData[0].eventAction, 'click');
assert.equal(hitData[0].eventLabel, 'http://google-analytics.com/collect');
Expand All @@ -74,7 +75,22 @@ describe('outboundLinkTracker', function() {
.execute(ga.getHitData))
.value;

assert(!hitData.length);
assert.equal(hitData.length, 0);
});


it('should not send events on non-http(s) protocol links', function*() {

var hitData = (yield browser
.execute(stopLinkClickEvents)
.execute(stubBeacon)
.execute(ga.run, 'require', 'outboundLinkTracker')
.click('#javascript-protocol')
.click('#file-protocol')
.execute(ga.getHitData))
.value;

assert.equal(hitData.length, 0);
});


Expand All @@ -89,7 +105,7 @@ describe('outboundLinkTracker', function() {
.execute(ga.getHitData))
.value;

assert(!hitData.length);
assert.equal(hitData.length, 0);
});


Expand Down

0 comments on commit 5acf843

Please sign in to comment.