Microsoft Clarity in Azure AD B2C
Tags: azure, clarity, microsoftclarity, b2c
Published: 2024-03-01 15:33:54

Have you heard about Microsoft Clarity before?

Microsoft's official definition is 

> 
_Clarity is a free user behavior analytics tool that helps you understand how users are interacting with your website through session replays and heatmaps._

What about Azure AD B2C?
>
_Azure AD B2C is a customer identity access management (CIAM) solution capable of supporting millions of users and billions of authentications per day. It takes care of the scaling and safety of the authentication platform, monitoring, and automatically handling threats like denial-of-service, password spray, or brute force attacks._

I've been growing quite fond of Clarity recently! It's an amazing tool and will always be free! Super simple to get started as well;
- Add your site to https://clarity.microsoft.com/
- Get the tracking script of your choice
- Put the script into the head element of your view page.

So far so good. 

I also wanted to add this script to a user flow we had in place including Azure B2C as the login process.

Here are some of the contexts you need to be aware of before we start how to achieved this.

The project uses Sign In user flow, as well as one of the default page layouts that Azure B2C offers, with nothing special within either. Configuration is detailed [here](https://learn.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-user-flows?pivots=b2c-user-flow), and pick a template after creating your user flow. Check the [documentation](https://learn.microsoft.com/en-us/azure/active-directory-b2c/customize-ui?pivots=b2c-user-flow) on how to do it. 

![Default templates in the dropdown](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oczjr4mqeixjifkyjxo0.png)

While we want to keep the template, we also want to inject the Clarity script into these pages that have been generated for us. 

## Here are the steps to achieve this;

1- Enable JavaScript enforcing
Go to the properties of your user flow, enable the JavaScript enforcing

![JavaScript enabling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xe50x7qrg2gov9652jco.png)

2- Download the default page layout template you want to keep using
[Here](https://github.com/Azure-Samples/Azure-AD-B2C-page-templates) is the GitHub repo for the default page layout templates

3- Edit the relevant HTML page by adding your script to the head element of your template, if you do not need to, you do not have to touch the rest of the page!

![Script in the HTML page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlc5wzdo412qj0x4kkhe.png)

4- Now host these wanted HTML files somewhere. Can be in blob storage, can be within your code, or wherever you want them to be.
For the sake of this article, I'm going to share the approach Microsoft Docs suggested, [hosting with an Azure Blob Storage](https://learn.microsoft.com/en-us/azure/active-directory-b2c/customize-ui-with-html?pivots=b2c-user-flow#2-create-an-azure-blob-storage-account)
If you're choosing to host the HTML files in the blob storage do not forget to configure CORS!

5- Finally update your user flow page layouts with the blob storage hosted HTML file URLs!


There it is! It might take a little while for tracking to kick in, but you now can see your B2C steps in the Clarity!

Happy hacking!