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

Add outbound click event tracking for project URL, license URL, and manual download #4240

Merged
merged 2 commits into from
Jun 22, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/NuGetGallery/App_Code/ViewHelpers.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,17 @@
var config = DependencyResolver.Current.GetService<IGalleryConfigurationService>();
if(config != null) {
var propertyId = config.Current.GoogleAnalyticsPropertyId;
if (propertyId != null)
if (!string.IsNullOrEmpty(propertyId))
{
@Analytics.GetGoogleAsyncHtml(propertyId)
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', '@propertyId', 'auto');
ga('send', 'pageview');
</script>
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/NuGetGallery/Scripts/nugetgallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
attachPlugins();

sniffClickonce();

addOutboundTrackingEvent();
});

// Add validator that ensures provided value is NOT equal to a specified value.
Expand All @@ -24,6 +26,38 @@
$.validator.unobtrusive.adapters.addBool("mandatory", "required");
$.validator.unobtrusive.adapters.addSingleVal('notequal', 'disallowed');

// Source: https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits
function createFunctionWithTimeout(callback, opt_timeout) {
var called = false;
function fn() {
if (!called) {
called = true;
callback();
}
}
setTimeout(fn, opt_timeout || 1000);
return fn;
};

function addOutboundTrackingEvent() {
// Handle Google analytics tracking event on specific links.
$.each($('a[data-track]'), function () {
$(this).click(function (e) {
var href = $(this).attr('href');
var category = $(this).attr('data-track');
if (ga && href && category) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need a e.preventDefault() here? Otherwise wouldn't the link's default behavior just leave the page without waiting for the hitCallback?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch. Adding this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

e.preventDefault();
ga('send', 'event', category, 'click', href, {
'transport': 'beacon',
'hitCallback': createFunctionWithTimeout(function () {
document.location = href;
})
});
}
});
});
}

function padInt(i, size) {
var s = i.toString();
while (s.length < size) s = "0" + s;
Expand Down
6 changes: 3 additions & 3 deletions src/NuGetGallery/Views/Packages/DisplayPackage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@
@if (PackageHelper.ShouldRenderUrl(Model.ProjectUrl))
{
<li>
<a href="@Model.ProjectUrl" title="Visit the project site to learn more about this package" rel="nofollow">
<a href="@Model.ProjectUrl" data-track="outbound-project-url" title="Visit the project site to learn more about this package" rel="nofollow">
Project Site
</a>
</li>
}
@if (PackageHelper.ShouldRenderUrl(Model.LicenseUrl))
{
<li><a href="@Model.LicenseUrl" title="Make sure you agree with the license" rel="nofollow">License</a></li>
<li><a href="@Model.LicenseUrl" data-track="outbound-license-url" title="Make sure you agree with the license" rel="nofollow">License</a></li>
}
<li><a href="@Url.Action(actionName: "ContactOwners", controllerName: "Packages", routeValues: new { id = Model.Id })" title="Ask the package owners a question">Contact Owners</a></li>
@if ((Model.IsOwner(User) || User.IsAdministrator()))
Expand All @@ -82,7 +82,7 @@

@if (!Model.Deleted)
{
<li><a href="@Url.PackageDownload(2, Model.Id, Model.Version)" title="Download the raw nupkg file." rel="nofollow">Download</a> <a href="@Url.Action(actionName: "Download", controllerName: "Packages")" title="How do I download?">(how-to)</a></li>
<li><a href="@Url.PackageDownload(2, Model.Id, Model.Version)" data-track="outbound-manual-download" title="Download the raw nupkg file." rel="nofollow">Download</a> <a href="@Url.Action(actionName: "Download", controllerName: "Packages")" title="How do I download?">(how-to)</a></li>
<li><a class="s-clickonce" href="@Url.ExplorerDeepLink(2, Model.Id, Model.Version)" title="Explore the nupkg with the NuGet Package Explorer (IE only)" rel="nofollow">Open in Package Explorer</a></li>
}

Expand Down