Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracks external link clicks using Google Analytics #2135

Merged
merged 2 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Adds the ability to track outbound links for usage analysis [#2135](https://github.com/sul-dlss/SearchWorks/pull/2135)
### Changed
- Now returning a 404 Not Found error when an article record is not found [#2133](https://github.com/sul-dlss/SearchWorks/pull/2133)
- Updates the way that Hoover Archive records are requested and shown in the access panel [#2123](https://github.com/sul-dlss/SearchWorks/pull/2123)
Expand Down
18 changes: 17 additions & 1 deletion app/assets/javascripts/analytics.js
Expand Up @@ -60,5 +60,21 @@ Blacklight.onLoad(function(){
$('.iiif-dnd').on('dragstart', function(e) {
var manifest = $(e.currentTarget).data().manifest;
window._gaq.push(['_trackEvent', 'IIIF DnD', 'dragged', manifest]);
})
});

// Track external link clicks
$('a[href]:not([href^="/"],[href^="#"])').on('click', function(e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be the correct selector. I'm not sure if there are any other considerations we should make here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will end up tracking any time we use a fully qualified URL? I don't think we use that a lot, but just want to make sure I know the considerations we may have to take moving forward.

e.preventDefault();
e.stopPropagation();
var _this = this;
var link = $(e.currentTarget);
try {
if (link[0].host !== window.location.host) {
window._gaq.push(['_trackEvent', 'External link', 'clicked', link.attr('href')]);
}
}
finally {
window.location.href = _this.href;
}
});
});