Skip to content

Commit 32bf908

Browse files
committed
Emit Google Analytics click event on middle-mouse click (#7249)
Address #7248
1 parent 9697b48 commit 32bf908

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

src/NuGetGallery/Scripts/gallery/common.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@
406406
if (window.nuget.isGaAvailable()) {
407407
ga('send', 'event', category, action, label, eventValue, options);
408408
}
409-
}
409+
};
410410

411411
window.nuget = nuget;
412412

@@ -447,25 +447,39 @@
447447
.focus();
448448

449449
// Handle Google analytics tracking event on specific links.
450+
var emitClickEvent = function (e, emitDirectly) {
451+
if (!window.nuget.isGaAvailable()) {
452+
return;
453+
}
454+
455+
var href = $(this).attr('href');
456+
var category = $(this).data().track;
457+
var trackValue = $(this).data().trackValue;
458+
if (href && category) {
459+
if (emitDirectly) {
460+
window.nuget.sendAnalyticsEvent(category, 'click', href, trackValue);
461+
} else {
462+
// This path is used when the click will result in a page transition. Because of this we need to
463+
// emit telemetry in a special way so that the event gets out before the page transition occurs.
464+
e.preventDefault();
465+
window.nuget.sendAnalyticsEvent(category, 'click', href, trackValue, {
466+
'transport': 'beacon',
467+
'hitCallback': window.nuget.createFunctionWithTimeout(function () {
468+
document.location = href;
469+
})
470+
});
471+
}
472+
}
473+
};
450474
$.each($('a[data-track]'), function () {
451-
$(this).click(function (e) {
452-
var href = $(this).attr('href');
453-
var category = $(this).data().track;
454-
var trackValue = $(this).data().trackValue;
455-
if (window.nuget.isGaAvailable() && href && category) {
456-
if (e.altKey || e.ctrlKey || e.metaKey) {
457-
window.nuget.sendAnalyticsEvent(category, 'click', href, trackValue);
458-
} else {
459-
e.preventDefault();
460-
window.nuget.sendAnalyticsEvent(category, 'click', href, trackValue, {
461-
'transport': 'beacon',
462-
'hitCallback': window.nuget.createFunctionWithTimeout(function () {
463-
document.location = href;
464-
})
465-
});
466-
}
475+
$(this).mouseup(function (e) {
476+
if (e.which === 2) { // Middle-mouse click
477+
emitClickEvent.call(this, e, true);
467478
}
468479
});
480+
$(this).click(function (e) {
481+
emitClickEvent.call(this, e, e.altKey || e.ctrlKey || e.metaKey);
482+
});
469483
});
470484

471485
// Show elements that require ClickOnce

0 commit comments

Comments
 (0)