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:

  1. A successfully set up Hugo blog website. You can refer to previous articles for corresponding tutorials.
  2. A Microsoft Clarity account (can be registered at Clarity official website).
  3. Basic knowledge of Hugo configuration files and themes.

Step 1: Get the Clarity Tracking Code

  1. 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:

      HTML
      <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>
      Click to expand and view more
    • 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.

YAML
  analytics:
    enabled: true
    baidu:
      enabled: false
      id: "your-baidu-analytics-id"
    clarity:
      enabled: true
      id: "your_project_id"
Click to expand and view more

This completes the deployment.

Method 2: Directly Edit the Theme’s head.html

  1. Locate the head.html file in the Theme:

    • Open the themes/your-theme/layouts/partials/ directory of the Hugo project and find head.html or a similar file (the file name may vary with different themes, such as header.html).
    • If your theme does not have head.html, you can check the layouts/_default/baseof.html file.
  2. Add the Clarity Tracking Code:

    • Paste the Clarity-provided tracking code snippet inside the <head> tag of the head.html file. For example:

      HTML
      {{ 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 }}
      Click to expand and view more
    • 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.

  3. 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.

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.

  1. Edit config.toml or config.yaml:

    • Open the config.toml (or config.yaml) file in the root directory of the Hugo project.

    • Add the Clarity tracking code snippet under the [params] section. For example, in config.toml:

      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>
          """
      Click to expand and view more
    • If using config.yaml, add:

      YAML
      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>
      Click to expand and view more
  2. Modify Theme Template to Include Custom HTML:

    • In themes/your-theme/layouts/partials/head.html, add the following code to reference the customHeadHTML from the configuration file:

      HTML
      {{ if .Site.Params.customHeadHTML }}
          {{ .Site.Params.customHeadHTML | safeHTML }}
      {{ end }}
      Click to expand and view more
    • This ensures the Clarity tracking code is correctly loaded into the <head> tag.


Step 3: Verify Clarity is Working Properly

  1. 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).
  2. 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

  1. Avoid Local Development Interference:

    • As mentioned above, use {{ if not .Site.IsServer }} to prevent tracking data interference in local development mode.
  2. 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.
  3. Integration with Google Analytics:

    • Clarity’s heatmap and session recording features complement Google Analytics’ traffic analytics. You can use both for comprehensive analysis data.
  4. 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.


References

Copyright Notice

Author: heyjude

Link: https://heyjude.blog/posts/hugo_clarity_integration/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Comments

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut