What is a good way of track link clicks in Asp.Net Core 7 MVC app in Application Insights?

Astha Gangwar 0 Reputation points
2024-05-20T07:49:05.8166667+00:00

I have an MVC app with several <a> tags, and I want to have a way of tracking link clicks, preferably without doing redirects (i.e., without changing link addresses). Is there any way this can be done? and the link clicks are tracked on Application Insights. And, if not, is there any way one can get this functionality with some work around?

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,920 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,283 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 49,701 Reputation points
    2024-05-20T14:33:04.3966667+00:00

    AI allows custom events. We don't use AI directly but we use Google Analytics and we have the same requirement. What you do is have your link call a javascript function in your code that records the event in AI (from the client side) and then redirect. This doesn't change the link, it just changes it to call JS first.

    If you need to do this for a lot of links (or all of them) then I would recommend you create a generally reusable function that accepts the URL (and possibly additional data if you need more info) and generates the event followed by the redirect. Then either update all your links to call the helper function (if there are a couple) or use jQuery (or equivalent) to inject the call into your links after the document loads.

    0 comments No comments

  2. Bruce (SqlWork.com) 59,036 Reputation points
    2024-05-20T22:00:43.68+00:00

    you would use the javascript application insights sdk:

    https://github.com/microsoft/ApplicationInsights-JS

    note: net 7 support ended 5/14/2024

    0 comments No comments

  3. Fei Han - MSFT 306 Reputation points Microsoft Vendor
    2024-06-10T05:03:54.9066667+00:00

    I want to have a way of tracking link clicks, preferably without doing redirects (i.e., without changing link addresses). Is there any way this can be done? and the link clicks are tracked on Application Insights.

    Hi @Astha Gangwar , you can try to enable and use the Click Analytics plug-in with Application Insights that can help automatically tracks click events on webpages.

    For more information about Click Analytics Plugin for the Application Insights Javascript SDK, please check this doc: https://learn.microsoft.com/en-us/azure/azure-monitor/app/javascript-feature-extensions?tabs=javascriptwebsdkloaderscript#add-the-code

    And to prevent <a> link from redirecting, you can call event.preventDefault() on click event of <a> tag.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments