Add specific value to any Link of Website in Sitecore


In my recent project I came across with a requirement where client need specific values or actually contactid of current visitor to added into specific links so that whenever user is clicking the link that will redirect user with contact id in query string and they can store that value for custom reporting.

Actual implementation need is client want to collect actual contact who clicked on the links and complete the transaction or functionality which they want user to do. So they just wanted to track the contacts/users it can be either logged in user or anonymous user.

And for this content editor can add to any link field General Link Field, General Link with Search, Richtext etc…

Also content editor should have functionality to add the link anywhere and it should work.

So I thought to add a token for content editor so that they can add the token to link they want to add contact id and from the backend we can replace the token with actual value.

Now to track this down and update the token value with the actual contact id I didn’t found any direct way to implement it so I started looking over the internet and found that I can update RenderField pipeline of Sitecore. There actually I can get the field value before rendering or serving it to page and I can make the change there.

I created a class file to make this change as below –


public class GetLinkFieldValueExtend
    {
        public void Process(RenderFieldArgs args)
        {
            Assert.ArgumentNotNull(args, nameof(args));
            if ((args.FieldTypeKey != "general link with search" && args.FieldTypeKey != "general link") || args.FieldTypeKey != "rich text" || string.IsNullOrWhiteSpace(args.Result.FirstPart))
            {
                return;
            }

            args.Result.FirstPart = UpdateToken(args.Result.FirstPart);
            args.Result.LastPart = args.Result.LastPart;
        }

        private string UpdateToken(string fieldValue)
        {
            if (fieldValue.Contains("$$contactid$$"))
            {
                fieldValue = GetTokenUpdateLink(fieldValue);
            }
            return fieldValue;
        }

        public static string GetTokenUpdateLink(string url)
        {

            try
            {
                var contactId = "contact id"; // write logic to get the contact id;
                return url.Replace(ConfigSettings.ContactIdTokenName, contactId);
            }
            catch (Exception ex)
            {
                Log.Error("Issue with update token value for link" + ex.Message, true);
            }
            return url;
        }

        /// <summary>
        /// Checks if the field should not be handled by the processor.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>true if the field should not be handled by the processor; otherwise false</returns>
        protected virtual bool SkipProcessor(RenderFieldArgs args)
        {
            if (args == null)
            {
                return true;
            }

            string fieldTypeKey = args.FieldTypeKey;
            if (fieldTypeKey != "link")
            {
                return fieldTypeKey != "general link";
            }

            return false;
        }

    }

After that I added config for this to execute this code to just after the RenderField processor.


<?xml version="1.0"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:env="http://www.sitecore.net/xmlconfig/env/">
  <sitecore>
    <pipelines>
      <renderField>
        <processor type="Xcentium.Foundation.SitecoreExtensions.Pipelines.RenderField.GetLinkFieldValueExtend, Xcentium.Foundation.SitecoreExtensions" patch:after="processor[@type='Sitecore.Buckets.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Buckets']">
        </processor>
      </renderField>
    </pipelines>
  </sitecore>
</configuration>

 

This is not for only link field you can use it with any field in Sitecore and make it more specific if you need.

 

Hope it can save some of your time 🙂

 

This entry was posted in Uncategorized, Sitecore and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s