Weekly change report with Application Insights and Logic Apps
application-insights logic-apps azure
Using Application Insights and Logic Apps to track changes in RSVP on my wedding website with weekly e-mail reports.
December 21, 2019
I'm getting married next year (yay! 🥳) and I have built a website for my wedding (of course). We use it to provide our guests with basic information about the event and also to track their attendance ("RSVP") and whether they will spend the night or not ("Overnight").
I quickly figured that we also need some kind of notification mechanism to let us know about changes happening on the site (guests confirming, changing their minds etc.). Since the site doesn't really have a database I thought it would be quite elegant to use Azure Application Insights together with Logic Apps to send weekly reports of changes in an e-mail. And this is the result:
Application Insights
My app is a standard ASP.NET Core web application with Razor pages frontend and underlying REST API endpoints to manage state.
Integrating Application Insights with this kind of application is quite simple and Visual Studio can handle everything automagically (just right click on the project in Solution Explorer and select Add Application Insights telemetry).
This NuGet will be installed:
Microsoft.ApplicationInsights.AspNetCore
This service will be added to Startup
:
services.AddApplicationInsightsTelemetry();
This section will be added to appsettings.json
:
"ApplicationInsights": {
"InstrumentationKey": "keykey-keykey-keykey"
}
And suddenly, the TelemetryClient
instance can be used in dependency injection.
To track changes in our guest's responses I'm using App Insights custom events with type as key and guest ID + new state as values.
private readonly TelemetryClient _telemetryClient; // assigned through DI
...
private void TrackChange(string type, string guestId, string newValue)
{
var telemetryValues = new Dictionary<string, string>()
{
{ "guestId", guestId },
{ "value", newValue }
};
_telemetryClient.TrackEvent(type, telemetryValues);
}
Every time our guest clicks one of the buttons on the page, the API is called and TrackChange
is triggered, logging this change into Application Insights.
Note to self: Using
value
as the key is not the smartest thing because other things in Application Insights are called "value" as well. Then it's hard to distinguish in Logic Apps which is which.
In App Insights analytics I can use this query to get a nice overview of logged changes:
customEvents
| project guestId=customDimensions.guestId, name, value=customDimensions.value, timestamp
| order by timestamp
Logic App
With the data source ready I could proceed to the e-mail report itself. I decided to use the Azure Logic Apps service because it greatly simplifies integration between Application Insights and Office 365.
These are the three main parts of my Logic App:
- Run Analytics query against Application Insights, range will be last 7 days
- Compile query results into string (HTML table)
- Send it as e-mail to myself and my wife-to-be 😊
And repeat this weekly.
Step 1: Recurrence
Step 2: Run Analytics query (it's the same query as above, I had to provide API key and application ID to authenticate):
I recommend running the Logic App manually at this point, so that you see the outcome of your analytics query and the app is aware of your data model.
Step 3: Initialize new string variable:
Step 4: Check if the query returned any results:
Function used here:
length(body('Run_Analytics_query')?['value'])
Step 5.1: Create HTML table header and for each line in query results create new table row and fill it with data* (Logic Apps understands my query and helps by suggesting column names):
This function is used to format date:
formatDateTime(items('For_each')?['timestamp'], 'yyyy-MM-dd HH:mm')
.
Step 5.2: Set the variable to "No changes" and be done with it.
Step 6: Put the result into an e-mail and send (I had to sign in with my Office 365 account first):
And that's about it. Every week I get this beautiful e-mail summarizing my guests' changes:
Feedback
Found something inaccurate or plain wrong? Was this content helpful to you? Let me know!
📧 codez@deedx.cz