Introduction
Microsoft Clarity is a free website analytics tool that provides user behavior heatmaps, session recordings, and detailed analysis data to help webmasters better understand user behavior, optimize website content, and improve user experience. After setting up your own blog website, you might want to track user behavior, and this tool can help you analyze user actions. For static blog websites built with Hugo, integrating Clarity is straightforward and can be done in just a few steps.
Prerequisites
Before getting started, you will need:
- A successfully set up Hugo blog website. You can refer to previous articles for corresponding tutorials.
- A Microsoft Clarity account (can be registered at Clarity official website).
- Basic knowledge of Hugo configuration files and themes.
Step 1: Get the Clarity Tracking Code
- Sign up and Create a Project:
-
Visit the Microsoft Clarity official website and log in with your Microsoft account.
-
Create a new project, enter your blog website URL (e.g.,
https://heyjude.blog
). -
After saving the project, Clarity will generate a tracking code snippet like this:
<script type="text/javascript"> (function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, "clarity", "script", "your_project_id"); </script>
-
Copy the
your_project_id
(Project ID) from this code, as you will need it later.
-
Step 2: Add Clarity Tracking Code to the Hugo Blog
Since we have already built our own blog based on Hugo, we know that Hugo is a static site generator, and all page content is generated through template files in the layouts
directory. We need to embed the Clarity tracking code into the <head>
tag of the website.
Method 1: Add Based on the Theme Used
Usually, we use pre-built themes to create our blogs, and some themes may already include Clarity code, requiring only the replacement of our project ID. In this case, the blogger uses the hugo-narrow theme, which includes Clarity’s code, and only the project ID needs to be replaced in the configuration.
analytics:
enabled: true
baidu:
enabled: false
id: "your-baidu-analytics-id"
clarity:
enabled: true
id: "your_project_id"
This completes the deployment.
Method 2: Directly Edit the Theme’s head.html
-
Locate the
head.html
file in the Theme:- Open the
themes/your-theme/layouts/partials/
directory of the Hugo project and findhead.html
or a similar file (the file name may vary with different themes, such asheader.html
). - If your theme does not have
head.html
, you can check thelayouts/_default/baseof.html
file.
- Open the
-
Add the Clarity Tracking Code:
-
Paste the Clarity-provided tracking code snippet inside the
<head>
tag of thehead.html
file. For example:{{ if not .Site.IsServer }} <script type="text/javascript"> (function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, "clarity", "script", "your_project_id"); </script> {{ end }}
-
Note: We use the
{{ if not .Site.IsServer }}
conditional statement to prevent loading the Clarity code in local development mode (hugo server
) to avoid interfering with local test data.
-
-
Save and Test:
- After saving the file, run
hugo server -D
to preview the website locally. - Deploy the website to production (e.g., GitHub Pages or Vercel) and then visit the Clarity dashboard to confirm if the data is being recorded.
- After saving the file, run
Method 3: Add via Hugo Configuration File
If you prefer not to directly modify theme files (e.g., for easier theme updates), you can add the Clarity code through Hugo’s configuration file.
-
Edit
config.toml
orconfig.yaml
:-
Open the
config.toml
(orconfig.yaml
) file in the root directory of the Hugo project. -
Add the Clarity tracking code snippet under the
[params]
section. For example, inconfig.toml
:[params] customHeadHTML = """ <script type='text/javascript'> (function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src='https://www.clarity.ms/tag/'+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, 'clarity', 'script', 'your_project_id'); </script> """
-
If using
config.yaml
, add:params: customHeadHTML: | <script type='text/javascript'> (function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src='https://www.clarity.ms/tag/'+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, 'clarity', 'script', 'your_project_id'); </script>
-
-
Modify Theme Template to Include Custom HTML:
-
In
themes/your-theme/layouts/partials/head.html
, add the following code to reference thecustomHeadHTML
from the configuration file:{{ if .Site.Params.customHeadHTML }} {{ .Site.Params.customHeadHTML | safeHTML }} {{ end }}
-
This ensures the Clarity tracking code is correctly loaded into the
<head>
tag.
-
Step 3: Verify Clarity is Working Properly
-
Deploy the Website:
- Generate static files using
hugo
and deploy to your hosting platform (e.g., GitHub Pages, Vercel, or Netlify). - Ensure the Clarity tracking code is correctly embedded in the generated HTML files (you can check the
<head>
tag using browser developer tools).
- Generate static files using
-
Check the Clarity Dashboard:
- Log in to the Clarity dashboard and wait a few minutes (typically 2 hours) to see if there is user visit data, heatmaps, or session recordings.
- If there is no data, check:
- If the project ID is correct.
- If the tracking code is correctly embedded in the
<head>
tag. - If the website has been deployed to the public domain.
Step 4: Optimization and Considerations
-
Avoid Local Development Interference:
- As mentioned above, use
{{ if not .Site.IsServer }}
to prevent tracking data interference in local development mode.
- As mentioned above, use
-
Privacy and Compliance:
- Clarity collects user behavior data, so ensure your website’s privacy policy mentions the use of Clarity for analysis.
- If your blog targets EU users, comply with GDPR requirements, which may involve adding a cookie consent prompt.
-
Integration with Google Analytics:
- Clarity’s heatmap and session recording features complement Google Analytics’ traffic analytics. You can use both for comprehensive analysis data.
-
Regular Data Checking:
- Periodically review the Clarity dashboard’s heatmaps and session recordings to analyze user click behavior and page scroll depth, optimizing blog content layout.
Comments