How do I retrieve classic app insights pageview data on Azure Functions with JavaScript?

Winston Loh 10 Reputation points

Hello there!

I'm looking to retrieve pageView data from a Classic Application Insights on Azure Functions with JavaScript.

Due to project constraints I'm yet not able to migrate to a workspace-based App Insights, so I'm not too sure else what I'll need to accomplish the above.

What I've found out so far:

  • I'm looking at using the @azure/monitor-query library to make KQL queries like I did in Azure Portal to get pageView data, but am not sure what I'll need to use (e.g. API access key maybe? But then I'm unsure which @azure/identity credential I'll need to use if so.)
  • The example provided uses workspace IDs (does the @azure/monitor-query library work for classic app insights, or would I be able to find a workspace ID associated with a particular classic app insights instance?)

Really appreciate your time reading this, and thanks in advance! :)

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,018 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,643 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
965 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Winston Loh 10 Reputation points

    If you are in a squeeze to use Classic App Insights before its retirement in Feb 2023, I've just figured it out from digging bits of some old docs I've found in this other question and a mix of other sources like this one:

    1. REST API will have to do, because of the impending retirement
    2. Navigate to the 'API Access' blade for the classic app insights instance in Azure Portal
    3. Create an API key (The solution is short-lived and will be replaced upon migrating to workspace-based app insights anyway, so I didn't have to worry about the use of API keys ending in March 2026, though the Entra method will be the way to go in the newer solution that uses the JavaScript SDK.)
    4. Keep the API Key somewhere safe and reasonable
    5. Return to API Access blade and take note of the App ID
    6. Make a HTTP request (in your application code)
      • URL:<app-id-here>/query
      • Include the API key as a HTTP header 'X-Api-Key'
      • include the necessary KQL query as a parameter (?query=<your KQL here>)
      • in my case it was the pageViews table that i needed, plus some massaging (?query=pageViews | where name has "My Page Name" | summarize count() by startofmonth(timestamp))
    1 person found this answer helpful.