Skip to content

Commit f87daf5

Browse files
committed
checkpoint
1 parent 9c985dd commit f87daf5

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

articles/search/search-traffic-analytics.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,25 @@ The following screenshot shows the built-in reports and charts for analyzing sea
197197

198198
## Example
199199

200-
**Create your first search app in C#** is an ASP.NET Core solution that you can use to practice adding instrumentation code.
200+
**Create your first search app in C#** is an ASP.NET Core sample that you can use to practice adding instrumentation code.
201201

202-
Use the sample code from the last lesson, [5 - Order results](https://docs.microsoft.com/en-us/azure/search/tutorial-csharp-orders), so that you can leverage search rank and more click behaviors. For this lesson, the [sample code](https://github.com/Azure-Samples/azure-search-dotnet-samples/tree/master/create-first-app/5-order-results) is located on GitHub.
202+
We recommend using the sample code from [Lesson 5 - Order results](tutorial-csharp-orders.md). It adds search rank, providing a richer baseline for data collection. For this lesson, the [sample code](https://github.com/Azure-Samples/azure-search-dotnet-samples/tree/master/create-first-app/5-order-results) is located on GitHub.
203203

204-
1. Before adding Application Insights and instrumentation code, run the program to make sure there are no build errors.
204+
1. Before adding Application Insights and instrumentation code, open **OrderResults.sln** in Visual Studio and run the program to make sure there are no build errors.
205205

206-
1. In Visual Studio, select **Project** > **Add Application Insights Telemetry**. For more information, see [Enable Application Insights server-side telemetry](https://docs.microsoft.com/azure/azure-monitor/app/asp-net-core#enable-application-insights-server-side-telemetry-visual-studio).
206+
1. Select **Project** > **Add Application Insights Telemetry**.
207207

208208
1. Click **Get Started**.
209209

210210
1. Select your subscription, account, resource, and click **Register***.
211211

212-
At this point, your application is set up for application monitoring, which means all page loads are tracked with default metrics.
212+
At this point, your application is set up for application monitoring, which means all page loads are tracked with default metrics. For more information about the previous steps, see [Enable Application Insights server-side telemetry](https://docs.microsoft.com/azure/azure-monitor/app/asp-net-core#enable-application-insights-server-side-telemetry-visual-studio).
213213

214214
1. Open **HomeController.cs**.
215215

216-
1. On line 52, add `private static TelemetryClient _telemetryClient;` and when prompted, add `using Microsoft.ApplicationInsights;` as an assembly reference.
216+
1. On line 267, add `private static TelemetryClient _telemetryClient;` and when prompted, add `using Microsoft.ApplicationInsights;` as an assembly reference.
217217

218-
1. On line 20, add a constructor that accepts a telemetry client:
218+
1. On line 44, add a constructor that accepts a telemetry client:
219219

220220
```csharp
221221
public HomeController(TelemetryClient telemetry)
@@ -224,22 +224,43 @@ Use the sample code from the last lesson, [5 - Order results](https://docs.micro
224224
}
225225
```
226226

227-
1. Correlate search and clicks events logged to Application Insights using the search ID. On line 79, add the following lines. When prompted, add `using System.Collections.Generic;` as an assembly reference.
227+
1. Next, correlate search events and clicks events through the search ID. On line 191, add the following lines to your query logic.
228228

229229
```csharp
230+
// Search Traffic Analytics: Establish a search ID used to correlate events
230231
var headers = new Dictionary<string, List<string>>() { { "x-ms-azs-return-searchid", new List<string>() { "true" } } };
231232

232-
var response = await client.Documents.SearchWithHttpMessagesAsync(searchText: searchText, searchParameters: parameters,
233-
customHeaders: headers);
233+
var response = await client.Documents.SearchWithHttpMessagesAsync(searchText: searchText, searchParameters: parameters, customHeaders: headers);
234234

235-
IEnumerable<string> headerValues;
236235
string searchId = string.Empty;
237-
if (response.Response.Headers.TryGetValues("x-ms-azs-searchid", out headerValues)){
236+
if (response.Response.Headers.TryGetValues("x-ms-azs-searchid", out IEnumerable<string>headerValues)){
238237
searchId = headerValues.FirstOrDefault();
239238
}
240239
```
241240

241+
1. Collect search events and click events. On line 202, add the following code.
242+
243+
```csharp
244+
// Search Traffic Analytics - Click events
245+
var clickProperties = new Dictionary<string, string> {
246+
{"SearchServiceName", "searchServiceName"},
247+
{"SearchId", "searchId"},
248+
{"ClickedDocId", "HotelId"}
249+
};
250+
_telemetryClient.TrackEvent("Click", clickProperties);
251+
252+
// Search Traffic Analytics - Query events
253+
var searchProperties = new Dictionary<string, string> {
254+
{"SearchServiceName", "searchServiceName"},
255+
{"SearchId", "searchId"},
256+
{"IndexName", "Hotel"},
257+
{"ScoringProfile", "profile"}
258+
};
259+
_telemetryClient.TrackEvent("Search", searchProperties);
260+
```
261+
242262
## Next steps
263+
243264
Instrument your search application to get powerful and insightful data about your search service.
244265

245266
You can find more information on [Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview) and visit the [pricing page](https://azure.microsoft.com/pricing/details/application-insights/) to learn more about their different service tiers.

0 commit comments

Comments
 (0)