DEV Community

Jesse Houwing
Jesse Houwing

Posted on • Originally published at jessehouwing.net on

Automatically add referral tag to URLs with Clipboard Fusion

Automatically add referral tag to URLs with Clipboard Fusion

Recently Microsoft introduced the Docs & Learn program. You, the MVP and content creator, get a unique identifier to add to any url linking to a number of Microsoft websites, and they automatically track the engagement you generate.

There's one small but, you need to add an identifier to every link you post. In my case to every link on:

  • This blog,
  • StackOverflow,
  • Microsoft forums,
  • GitHub wikis
  • Presentations shared on SlideShare
  • Etc.

And of course this technique works with any affiliate link program, so you can adapt it for the Amazon PartnerNet and many other sites as well.

While it isn't rocket science to edit a link, it can be tricky business. You have to juggle the ?, # and & just right and in the right order.

For example, to add my unique identifier to the following link:

https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2019#generated-code
Enter fullscreen mode Exit fullscreen mode

You can make quite a few mistakes:

❌ https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2019#generated-code?WT.mc_id=DOP-MVP-5001511
❌ https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2019#generated-code&WT.mc_id=DOP-MVP-5001511
❌ https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?WT.mc_id=DOP-MVP-5001511?view=vs-2019#generated-code
Enter fullscreen mode Exit fullscreen mode

The correct way to add the referral code is:

🎉 https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?WT.mc_id=DOP-MVP-5001511&view=vs-2019#generated-code
🎉 https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2019&WT.mc_id=DOP-MVP-5001511#generated-code

Enter fullscreen mode Exit fullscreen mode

Of course we all know this, right?! But still, it would be much nicer if one could automate this. To do that I turned to a trusty old tool called Clipboard Fusion. It's a nifty little tool that sits in your Windows task tray and manages the contents of your clipboard. One of it's feature is, that it can run a C# snippet on a trigger, so I set out to create such little snippet of magic, to automatically add my identifier to every suitable link I put on my clipboard.

using System;
using System.Web;

public static class ClipboardFusionHelper
{
    // Put your ID below
    public static string ID = "DOP-MVP-5001511";

    public static string[] docsAndLearnChampionDomains = { 
            "docs.microsoft.com",
            "learn.microsoft.com",
            "social.technet.microsoft.com",
            "azure.microsoft.com",
            "techcommunity.microsoft.com",
            "social.msdn.microsoft.com",
            "devblogs.microsoft.com",
            "developer.microsoft.com",
            "channel9.msdn.com",
            "gallery.technet.microsoft.com",
            "cloudblogs.microsoft.com",
            "technet.microsoft.com",
            "docs.azure.cn",
            "www.azure.cn",
            "msdn.microsoft.com",
            "blogs.msdn.microsoft.com",
            "blogs.technet.microsoft.com",
            "microsoft.com/handsonlabs"
        };

        public static string ProcessText(string text)
        {

            try
            {
                Uri x = new Uri(text, UriKind.Absolute);

                bool match = false;

                foreach (var domain in docsAndLearnChampionDomains)
                {
                    var y = new Uri("https://" + domain, UriKind.Absolute);
                    if (string.Equals(x.Host, y.Host, StringComparison.OrdinalIgnoreCase) 
                        && x.AbsolutePath.StartsWith(y.AbsolutePath, StringComparison.OrdinalIgnoreCase))
                    {
                        match = true;
                        break;
                    }
                }

                if (match)
                {
                    var uriBuilder = new UriBuilder(x);
                    var query = HttpUtility.ParseQueryString(uriBuilder.Query);
                    query["WT.mc_id"] = ID;
                    uriBuilder.Query = query.ToString();
                    return uriBuilder.Uri.AbsoluteUri;
                }
            }
            catch (UriFormatException) { }
            return text;
        }
}
Enter fullscreen mode Exit fullscreen mode

You can download the macro from here and use the Import option to add it to your instance of Clipboard Fusion.

Step-by-step adding the Macro to Clipboard Fusion.

Automatically add referral tag to URLs with Clipboard Fusion
Import the Macro into Clipboard Fusion

And then configure it as a new trigger on the Triggers tab:

Automatically add referral tag to URLs with Clipboard Fusion
Run the macro when the Trigger Command is received

Now every link you copy that matches the configured list of domains and paths will automatically be updated with your MVP ID before pasting it into your email, blog, forum answer or PowerPoint presentation.

Banner photo by ton.volz used under Creative Commons.

Top comments (0)