Capture and use UTM parameters in Sitecore Website

The most important thing as a web marketer is understanding your audience and knowing which types of promotions or marketing efforts are driving traffic to your website. UTM parameters are tags that you add to the end of your marketing URLs. They help track how visitors find your website and interact with your campaign. This is useful for identifying which channels are most effective.

In this post, I will share most commonly used UTM parameters and how you can capture them in your Sitecore website. How to process them by adding your custom business logic and pass them to other integrated systems like Forms or other lead tracking software’s.

Most commonly used UTM Parameters

There is no fixed rule or guidelines for UTM parameters and you can define your own parameters as well but there are five parameters which are most commonly used and recognised.

UTM ParameterExampleDescription
utm_sourceGoogle, Outlook, YahooWebsite the visitors are coming from.
utm_mediumppc, cpc, banner, and emailMarketing channels are bringing the visitor to your site.
utm_campaignspring_sale, summer-saleCampaign the promotion is associated with.
utm_termpopular+latestIdentify paid keywords you’re targeting with your campaign. 
utm_contentlogolink, callToActionIdentify the exact element on your ad or promotion that was clicked

Ref: https://doc.sitecore.com/search/en/developers/search-developer-guide/utm-parameters.html

You can see some more GA4 UTM Parameters on this link https://support.google.com/analytics/answer/10917952?hl=en#zippy=%2Cin-this-article

How to capture UTM parameters in Sitecore

There are many ways you can process UTM parameters. One of the most useful ways is as follows

  • On each external request it will capture the UTM parameters from query string and save into cookie
  • The advantage of using a cookie is that your Frontend code can also read and use them if needed.
  • You can use these to populate hidden values on forms or send them to external third party integrations.
using System;
using System.Collections.Specialized;
using System.Web;

namespace Demo
{
    public class UTMParametersProcessor
    {
        private const string CookieName = "UTMParameters";
        private const int CookieExpiryDays = 30;

        public static void ClearUTMParameters()
        {
            if (HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                HttpContext.Current.Response.Cookies[CookieName].Expires = DateTime.Now.AddDays(-1d);
            }
        }

        public static void SaveUTMParameters()
        {
            var urlReferrer = HttpContext.Current.Request?.UrlReferrer?.ToString() ?? string.Empty;

            // If the request is from same server and cookies is already set we will not process
            if (urlReferrer.Replace("http://", "").Replace("https://", "").StartsWith(HttpContext.Current.Request.Url.Host) &&
                HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                return;
            }

            NameValueCollection queryString = HttpContext.Current.Request.QueryString;
            var searchEngine = GetSearchEngineName(urlReferrer);


            HttpContext.Current.Response.Cookies[CookieName]["Source"] = GetParameterValue("utm_source", queryString, searchEngine);
            HttpContext.Current.Response.Cookies[CookieName]["Content"] = GetParameterValue("utm_content", queryString, searchEngine);
            HttpContext.Current.Response.Cookies[CookieName]["Campaign"] = GetParameterValue("utm_campaign", queryString, searchEngine);
            HttpContext.Current.Response.Cookies[CookieName]["Term"] = GetParameterValue("utm_term", queryString, searchEngine);
            HttpContext.Current.Response.Cookies[CookieName]["Medium"] = GetParameterValue("utm_medium", queryString, searchEngine);
            HttpContext.Current.Response.Cookies[CookieName].Expires = DateTime.Now.AddDays(CookieExpiryDays);

        }

        public static string GetSearchEngineName(string referrer)
        {
            string host = referrer.Replace("http://", "").Replace("https://", "").ToLower();
            int index = host.IndexOf("/");
            if (index > 0)
            {
                host = host.Substring(0, index);
            }

            //Remove the subdomain from the host
            string domain;
            if (host.IndexOf(".") != host.LastIndexOf("."))
            {
                string[] segments = host.Split('.');
                int n = segments.Length - 1;
                domain = segments[n - 1] + "." + segments[n];
            }
            else
            {
                domain = host;
            }

            return domain;
        }

        private static string GetParameterValue(string parameterName, NameValueCollection queryString, string searchEngine)
        {
            // Perform additional custom business logic here if needed

            if (searchEngine.ToLower().Equals("google"))
            {
                // change parameter or perform additional logic
            }

            return queryString[parameterName];
        }
    }
}

How to call this capture UTM Parameters method in Sitecore?

If you are using Webforms and have a layout linked to aspx page with code-behind file

  • You can call this method in Page_Load method. If you have multiple layouts you will need to call it in each layout example below
  • You can also create a custom Sublayout and then use Sitecore Presentation -> Details to add this page to selected layouts.
protected void Page_Load(object sender, EventArgs e)
{     
     Demo.UTMParametersProcessor.SaveUTMParameters();
        
     // additional code here

}

If you are using MVC layout

  • You can add this line below directly to you layout. If you have multiple layouts you will need to call it in each layout example
  • You can also create a custom rendering and using Sitecore Presentation details to add this component to all/selected pages.
@{ Demo.UTMParametersProcessor.SaveUTMParameters(); }

Improvements and Integrations

The code samples above will give you a starting point, you can off course change/update it as per your own requirements and business logic. The code can also be improved to create/update cookies only when needed. You can extend the UTMParametersProcessor to create a method that can read this data from cookie and return value in a custom object.

If you are using a custom form or Sitecore forms, you can have hidden fields on the form that will not be shown to the actual customers but save these parameters on load and then pass them to third party systems like Eloqua or other lead tracking software’s. Similarly you can save them in custom database or pass to external systems for further processing.

Keywords:
Web Marketing, Audience Understanding, Traffic Analysis, UTM Parameters, Sitecore, Marketing URLs, Campaign Tracking, Channel Effectiveness, Custom Business Logic, Lead Tracking, Google Analytics 4 (GA4), Cookie Utilization, Hidden Form Values, Third-Party Integrations, Webforms, MVC Layout, Sublayout, Custom Rendering, Code Sample, Page_Load Method

Hashtags:
#WebMarketing, #UTMParameters, #AudienceAnalysis, #TrafficTracking, #Sitecore, #MarketingStrategy, #CampaignEffectiveness, #BusinessLogic, #LeadTracking, #GoogleAnalytics, #CookieManagement, #FormTracking, #ThirdPartyIntegration, #WebDevelopment, #MVC, #CodeExamples, #DigitalMarketing, #CustomRendering, #WebForms, #PageLoad