Skip to content

Commit

Permalink
Added tracking user time spent on page in App Insight functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mshakirdfe committed Jan 8, 2025
1 parent def5a4f commit 436a11a
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
}
<script type="text/javascript" integrity="sha384-g/ZkzetdQypWdY0NBZT5r2L3BR9/hURD8OBcd1rEaBpgX6QC7EaTL+o+mzWrBcXW" crossorigin="anonymous" src="https://js.monitor.azure.com/scripts/b/ext/ai.clck.2.8.18.min.js"></script>
<script type="text/javascript" asp-add-nonce>
window.appInsights = {
connectionString: '@ApplicationInsightsOptions.Value.ConnectionString',
instrumentationKey: '@ApplicationInsightsOptions.Value.InstrumentationKey',
authenticatedUserId: '@authenticatedUserId'
}
(function () {
var appInsights = window.appInsights || function (config)
{
function r(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } }
var t = { config: config }, u = document, e = window, o = "script", s = u.createElement(o), i, f;
for (s.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace", "Dependency"]; i.length;)r("track" + i.pop());
return t
}({
connectionString: '@ApplicationInsightsOptions.Value.ConnectionString',
instrumentationKey: '@ApplicationInsightsOptions.Value.InstrumentationKey',
authenticatedUserId: '@authenticatedUserId',
});
window.appInsights = appInsights;
appInsights.trackPageView();
let startTime = new Date();
window.addEventListener('beforeunload', function (event)
{
let endTime = new Date();
let timeSpent = endTime - startTime;
appInsights.trackMetric({ name: "TimeSpentOnPage", average: timeSpent });
});
</script>
<script type="text/javascript" src="~/dist/application-insights.min.js" asp-add-nonce></script>

0 comments on commit 436a11a

Please sign in to comment.