Skip to content

Commit 4a5d7fa

Browse files
committed
Emit Application Insights metric from browser for search selection (#7288)
Address #7287
1 parent d8dc32d commit 4a5d7fa

4 files changed

Lines changed: 63 additions & 7 deletions

File tree

src/NuGetGallery/Scripts/gallery/common.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@
304304
return typeof ga === 'function';
305305
};
306306

307+
nuget.isAiAvailable = function () {
308+
return typeof window.appInsights === 'object';
309+
};
310+
307311
nuget.getDateFormats = function (input) {
308312
var datetime = moment.utc(input);
309313

@@ -408,6 +412,12 @@
408412
}
409413
};
410414

415+
nuget.sendAiMetric = function (name, value, properties) {
416+
if (window.nuget.isAiAvailable()) {
417+
window.appInsights.trackMetric(name, value, 1, value, value, properties);
418+
}
419+
};
420+
411421
window.nuget = nuget;
412422

413423
jQuery.extend(jQuery.expr[':'], {

src/NuGetGallery/Views/Shared/ListPackages.cshtml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,25 @@
8080

8181
@section bottomScripts {
8282
<script type="text/javascript">
83+
// Used to track how long the user waited before clicking a search selection.
84+
var pageLoadTime = Date.now();
8385
86+
// Used to track how many selections were made on this page. Multiple selections can happen if the user opens
87+
// a search selection in a new tab, instead of navigating away from this page.
88+
var sincePageLoadCount = 0;
8489
@if (!string.IsNullOrWhiteSpace(Model.SearchTerm) && (Model.PageIndex == 0 || Model.Items.Count() > 0))
8590
{
8691
var action = Model.IncludePrerelease ? "search-prerel" : "search-stable";
8792
// Emit an event representing the search page and the page index. This make it easier for the search selection
8893
// event to be correlated in Google Analytics.
8994
<text>
9095
window.nuget.sendAnalyticsEvent('search-page', '@action', @Html.Raw(Json.Encode(Model.SearchTerm)), @Model.PageIndex);
96+
window.nuget.sendAiMetric('BrowserSearchPage', @Model.PageIndex, {
97+
SearchTerm: @Html.Raw(Json.Encode(Model.SearchTerm)),
98+
IncludePrerelease: '@Model.IncludePrerelease',
99+
PageIndex: @Model.PageIndex,
100+
TotalCount: @Model.TotalCount
101+
});
91102
</text>
92103
}
93104
@@ -111,7 +122,42 @@
111122
queryString
112123
].join('');
113124
window.location.href = url;
114-
})
125+
});
126+
127+
var emitAiClickEvent = function () {
128+
if (!window.nuget.isAiAvailable()) {
129+
return;
130+
}
131+
132+
var $this = $(this);
133+
var data = $this.data();
134+
if ($this.attr('href') && data.track) {
135+
window.nuget.sendAiMetric('BrowserSearchSelection', data.trackValue, {
136+
SearchTerm: @Html.Raw(Json.Encode(Model.SearchTerm)),
137+
IncludePrerelease: '@Model.IncludePrerelease',
138+
PageIndex: @Model.PageIndex,
139+
TotalCount: @Model.TotalCount,
140+
ClickIndex: data.trackValue,
141+
PackageId: data.packageId,
142+
PackageVersion: data.packageVersion,
143+
UseVersion: data.useVersion,
144+
SincePageLoadMs: Date.now() - pageLoadTime,
145+
SincePageLoadCount: sincePageLoadCount
146+
});
147+
148+
sincePageLoadCount++;
149+
}
150+
};
151+
$.each($('a[data-track]'), function () {
152+
$(this).mouseup(function (e) {
153+
if (e.which === 2) { // Middle-mouse click
154+
emitAiClickEvent.call(this, e);
155+
}
156+
});
157+
$(this).click(function (e) {
158+
emitAiClickEvent.call(this, e);
159+
});
160+
});
115161
});
116162
</script>
117163
}

src/NuGetGallery/Views/Shared/_ListPackage.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
}
2727
}
2828

29-
3029
<article class="package" role="listitem">
3130

3231
<div class="row">
@@ -42,6 +41,7 @@
4241
@if (itemIndex.HasValue)
4342
{
4443
@:data-track="@eventName" data-track-value="@itemIndex"
44+
@:data-package-id="@Model.Id" data-package-version="@Model.Version" data-use-version="@Model.UseVersion"
4545
}
4646
>@Html.BreakWord(Model.Id)</a>
4747

src/NuGetGallery/Web.config

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@
492492
</system.webServer>
493493
<runtime>
494494
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
495-
<dependentAssembly>
496-
<assemblyIdentity name="NuGet.Services.Contracts" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
497-
<bindingRedirect oldVersion="0.0.0.0-2.50.0.0" newVersion="2.50.0.0"/>
498-
</dependentAssembly>
495+
<dependentAssembly>
496+
<assemblyIdentity name="NuGet.Services.Contracts" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
497+
<bindingRedirect oldVersion="0.0.0.0-2.50.0.0" newVersion="2.50.0.0"/>
498+
</dependentAssembly>
499499
<dependentAssembly>
500500
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
501501
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0"/>
@@ -850,4 +850,4 @@
850850
</listeners>
851851
</trace>
852852
</system.diagnostics>
853-
</configuration>
853+
</configuration>

0 commit comments

Comments
 (0)