如何在Application Insights中提取SharePoint站点的真实唯一用户?
Got it, let's break this down for both SharePoint Online (SPO) and SharePoint On-Premises scenarios—since they have different constraints and options for capturing real unique users in Application Insights.
The default user_Id in SPO's Application Insights data is anonymized, so it doesn't reflect actual unique users. Here's how to get around that:
1. Inject Custom Script to Capture SPO User Context
SPO exposes user details via the _spPageContextInfo global object, which you can leverage to send real user identifiers as custom properties to Application Insights. You'll need to add this script to your SPO pages (via SPFx web parts, user custom actions, or modern page script editors if your tenant allows):
window.addEventListener('load', function() { // Grab real user details from SPO's page context const realUserId = _spPageContextInfo.userId; // Unique AAD object ID const userUPN = _spPageContextInfo.userEmail; // User's email/UPN // Initialize or reference your existing Application Insights instance const appInsights = window.appInsights; if (appInsights) { // Send page view with custom user properties appInsights.trackPageView({ customProperties: { realUserId: realUserId, userUPN: userUPN } }); // Attach these properties to all future telemetry appInsights.addTelemetryInitializer((envelope) => { envelope.data.baseData.properties.realUserId = realUserId; envelope.data.baseData.properties.userUPN = userUPN; }); } });
2. Query for Real Unique Users with Kusto
Once the custom properties are flowing into Application Insights, use this Kusto query to calculate real unique users:
// Daily real unique users for SPO pageViews | where customProperties has 'realUserId' | summarize DailyUniqueUsers = dcount(customProperties.realUserId) by bin(timestamp, 1d) | order by timestamp asc
On-prem SharePoint has fewer restrictions on user data, so you can capture real user identifiers more directly:
1. Client-Side Tracking with SP Context
Similar to SPO, use the _spPageContextInfo object in client-side scripts to send user details to Application Insights. The script will look almost identical to the SPO version—since the page context object works the same way for on-prem sites.
2. Server-Side Telemetry with AD Identifiers
If your on-prem SP uses Windows Active Directory, you can capture user identities server-side (e.g., in web parts, custom pages, or event receivers) and attach them to telemetry:
using Microsoft.ApplicationInsights; using System.Web; var telemetryClient = new TelemetryClient(); var currentUser = HttpContext.Current.User; if (currentUser.Identity.IsAuthenticated) { // Set authenticated user ID to AD username telemetryClient.Context.User.AuthenticatedUserId = currentUser.Identity.Name; telemetryClient.Context.User.Id = currentUser.Identity.Name; // Track a page view or custom event telemetryClient.TrackPageView("On-Prem Home Page"); }
3. Kusto Query for On-Prem Unique Users
For on-prem, you can use either the custom client-side properties or the server-side user_AuthenticatedId field:
// Daily real unique users for on-prem SP union pageViews, customEvents | extend realUserId = coalesce(customProperties.realUserId, user_AuthenticatedId) | where isnotempty(realUserId) | summarize DailyUniqueUsers = dcount(realUserId) by bin(timestamp, 1d) | order by timestamp asc
Key Notes
- Privacy Compliance: Make sure capturing user identifiers aligns with your organization's privacy policies (e.g., GDPR, CCPA).
- SPO Script Deployment: For modern SPO pages, SPFx web parts are the most reliable way to inject custom scripts (since direct script editors are restricted in most tenants).
- Consistency: Use the same user identifier (e.g., AAD object ID for SPO, AD username for on-prem) across all telemetry to ensure accurate unique counts.
内容的提问来源于stack exchange,提问作者motionpotion

