Blog

Capturing the Referring URL on Postback in an Online Form in Kentico

by Jeff Steil on July 21, 2016

Capturing the Referring URL on Postback in an Online Form in Kentico

If you have ever wanted to capture the referring url in an online form in Kentico but found that you can't do it using the default Kentico macros because the referring url is lost on postback, we are going to show you how to write a custom macro to do just that.

The macro itself is just a single class, however, we are going to also create a custom namespace as a best practice to avoid any method collisions with other pre-defined Kentico macros.  We will be using the site name of MySite for all of the examples in this blog post and all code shown was written in Kentico 9.2.

Create a Macros folder under App_Code

Under the App_Code folder in Kentico, create a MySite folder.  Under MySite, create a Macros folder.  We create a custom folder for our site to keep the code base clean and make it easier to find our custom code later.  Putting our custom macros in a site specific folder also means they won't get potentially overwritten on an upgrade.


Create a Custom Namespace

Create a new class underneath the Macros folder - MySiteNamespace.cs.  This custom namespace will allow you to define any macros you write in that unique namespace to ensure they don't conflict with any Kentico or third-party macros.

 

using CMS.MacroEngine;

namespace MySite.Macros
{
    public class MySiteNamespace : MacroNamespace
    {
    }
}

Create a Macro Loader Class

Create a new class underneath the Macros folder - MySiteMacroLoader.cs.  This class is needed to register your custom namespace into the the macro engine.

using CMS.Base;
using CMS.MacroEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MySite.Macros
{
    [MySiteMacroLoader]
    public class CMSModuleLoader
    {
        private class MySiteMacroLoader : CMSLoaderAttribute
        {
            public override void Init()
            {
                // Registers "MySiteNamespace" into the macro engine
                MacroContext.GlobalResolver.SetNamedSourceData("MySite", MySiteNamespace.Instance);
            }
        }
    }
}

Create the Macros Class

Create a new class underneath the Macros folder - MySiteMacros.cs.  This is where your custom macros will go.  In our example we only have a single macro, SaveFormReferrer(), but this class could contain many custom macros.

Because we are specifically looking to capture the referring url on a postback, we capture the referrer on page load and save it in a cookie wtih a key unique to that page.  Then on the postback we check to see if the cookie exists, and if it does, the referring url value of the cookie is returned.

using CMS;
using CMS.DocumentEngine;
using CMS.MacroEngine;
using MySite.Macros;
using System;
using System.Web;

[assembly: RegisterExtension(typeof(MySiteMacros), typeof(MySiteNamespace))]

public class MySiteMacros : MacroMethodContainer
{
    [MacroMethod(typeof(string), "Capture the referring url for an online form.", 1)]
    public static object SaveFormReferrer(EvaluationContext context, params object[] parameters)
    {
        //The referring url key includes the relative url of the page.
        string key = String.Format("{0}_referrer", DocumentContext.CurrentDocument.RelativeURL);

        //Store the referring url in a cookie if the request is a GET.
        if (HttpContext.Current.Request.HttpMethod == "GET")
        {
            //Set the saved referring url if there is one.
            HttpCookie cookie = HttpContext.Current.Response.Cookies[key];

            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                cookie.Value = HttpContext.Current.Request.UrlReferrer.ToString();
            }
        }
        else if (HttpContext.Current.Request.HttpMethod == "POST")
        {
            //If the request is a post, see if a referrer cookie exists.
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];

            //If the cookie exists, return the saved referring url.
            if (cookie != null)
            {
                return HttpContext.Current.Request.Cookies[key].Value;
            }
        }

        return String.Empty;
    }
}

Capturing the Referring URL in an Online Form

Now that we have the macro set up and ready to capture referring urls, we need to add a field to the online form to hold the referring url value.

In the Forms application, edit the form you want to add the referring url to.  For this example we are using the pre-defined Contact Us in the Corporate website.


Click on the Fields link in the left navigation.

Add a new field for the referring url.

  • Field Name: ReferringUrl
  • Data Type: Text
  • Size: 500
  • Required: false
  • Default Value: MySite.SaveFormReferrer()
  • Display field in the editing form: false

The field should not be required because it may not have a value (i.e. there may not be a referring url) and we do not want to display the field in the editing form because this will never be edited or entered by an administrator.


The Default Value of MySite.SaveFormReferrer() (don't forget to include the opening and closing curly brackets as shown above) is the key to saving the referring url.  The custom macro you wrote in the steps above will be called and will save the referring url in a cookie when the page loads.


When the form is submitted on a postback, the SaveFormReferrer() macro will save the referring url along with the other form fields to the database.

We needed to capture the referring url because we were passing it (along with the rest of the form values) to a CRM but you can use this approach to capture the referring url on any form.

 

#macro #referrer #referring #url

Comments
Blog post currently doesn't have any comments.
 Security code