I am still in the middle of implementing the App Insights JS SDK for my use, but below is the cmdlet I have built - and when each page or element loads I call this cmdlet like this.
Invoke-AppInsights -InsightType
When the page loads and right at the top I call
Invoke-AppInsights -InsightType 'Page' -PageName "Page 1"
I have a custom page name being sent to the JS SDK due to the fact that the SDK will use the web page <‘title’> which is different than the page name I want to send to App Insights.
Then within my other elements like grids I have cmdlets that pull specific data (such as VMs in an Azure Subscription). so if the cmdlet returns the correct data then I create an App Insights Event.
Invoke-AppInsights -InsightType 'Event' -EventData $EventData -EventName "GET $($FunctionUrl)" -EventDuration $($Duration.Milliseconds) -EventType 'HTTP' -EventMethod 'GET' -EventResponse '200'
else if the return data is not expected then I throw an exception to App Insights
Invoke-AppInsights -InsightType 'Exception' -EventData $EventData -EventName "GET $($FunctionUrl)" -ExceptionMessage $($_.Exception.Message) -EventType 'HTTP' -EventMethod 'GET' -EventResponse '400'
Full cmdlet - but its still WIP
function Invoke-AppInsights {
param($InsightType,$PageName,$EventName,$EventData,$EventType,$EventDuration,$EventMethod,$EventResponse,$ExceptionMessage)
if ($InsightType -eq 'Page' -and $PageName) {
Invoke-UDJavaScript -Javascript "aisdk.trackPageView({ name: '$($PageName)' });aisdk.setAuthenticatedUserContext('$($User)');"
} elseif ($InsightType -eq 'Event' -and $EventData) {
Invoke-UDJavaScript -JavaScript "
aisdk.trackDependencyData({
duration: '$($EventDuration)',
name: '$($EventName)',
responseCode: '$($EventResponse)',
id: '$(New-Guid)',
type: '$($EventType)',
method: '$($EventMethod)',
commandName: '$($EventName)',
properties: {
type: '$($EventData.type)',
id: '$($EventData.id)',
title: '$($EventData.title)',
}
});
aisdk.flush();
"
} elseif ($InsightType -eq 'Exception') {
Invoke-UDJavaScript -JavaScript "
aisdk.trackDependencyData({
duration: '$($EventDuration)',
name: '$($EventName)',
responseCode: '$($EventResponse)',
id: '$(New-Guid)',
type: '$($EventType)',
method: '$($EventMethod)',
properties: {
type: '$($EventData.type)',
id: '$($EventData.id)',
title: '$($EventData.title)',
'exception':'$($ExceptionMessage)'
}
});
aisdk.trackException({
exception: new Error('$($ExceptionMessage)')
});
aisdk.flush();"
}
}