Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Carlos Mosqueda 239 posts 429 karma points
    Jun 09, 2020 @ 16:30
    Carlos Mosqueda

    UmbracoUrlAlias and C# Query string builder, using Google UTM already in Query


    I have some using Umbraco (this may just be a general C# quesiton) that uses "umbracoUrlAlias", which is a shortcut URL for the same page. Anyways, what I do is take the "umbracoUrlAlias" and redirect to the actual page's URL and I append the "umbracoUrlAlias" to the end of the base page URL via a Query string for tracking.

    However, I am going to have our content authors start using Google UTMs for their links from external campaigns. This poses a problem, because if the URL I am redirecting to already has a query string (?alturl=Foo), I need to now if there is already a query string and then append the UTM query string (unknown when it comes in) onto the end of my URL. Example:

    Alt URL is this:

    and I redirect it to the actual page of:

    But a UTM page would come in as:

    But THEN I want the URL to redirect to URL by appending the UTM code to the end of my known URL:

    My current redirect code is:

         var currentUri = HttpContext.Current.Request.Url;
       var port = (currentUri.Port == 80) ? "" : ":" + currentUri.Port;
      var siteUrl = currentUri.Scheme + Uri.SchemeDelimiter + currentUri.Host;
     var canonicalUrl = Model.Content.Url;
      var theUrl = siteUrl + Model.Content.Url;
       var theRedirectUrl = "";
       // use Umbraco Url alias as the canonical url if it is set
          if (Model.Content.HasValue("umbracoUrlAlias"))
             // umbracourlalias can be a comma delimited string of alterantive urls, the canonical url will be the first
            var canonicalUrls = Model.Content.GetPropertyValue<string>("umbracoUrlAlias").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
           canonicalUrl = canonicalUrls.FirstOrDefault(); // this needs to take into account all redirects
    if (!String.IsNullOrEmpty(canonicalUrl))
        if (!canonicalUrl.StartsWith("/"))
            canonicalUrl = "/" + canonicalUrl;
      //keep consistent with how your site Urls ending, eg add a slash if that is the convention you are implementing
        foreach(var urlAlias in canonicalUrls){
        <input type="hidden" value="@urlAlias" />
          canonicalUrl = siteUrl + "/" + urlAlias;
          <input type="hidden" value="@canonicalUrl" />
             if(currentUri.ToString() == canonicalUrl){
               theRedirectUrl = theUrl;
               Response.Redirect(theRedirectUrl + "?vanityurl=" + urlAlias);

    My question would be how would I take the URL then return the URL and then append the UTM to the end like the examples above.

    I saw some C# UriBuilder properties. Is that a better way to go?

  • Amir Khan 1276 posts 2712 karma points
    Jun 09, 2020 @ 19:02
    Amir Khan

    Out of curiosity, why do you need to preserve the utm query string when it has already fired before the redirect?

  • Carlos Mosqueda 239 posts 429 karma points
    Jun 09, 2020 @ 19:26
    Carlos Mosqueda

    Hi Amir,

    It actually doesn't. Or maybe I am just overly complicating this.

    So what I have is a property using the 'umbracoUrlAlias',
    So my page, also has an 'umbracoUrlAlias' of "awesome".

    So when Umbraco sees the URL "/awesome" it renders the same content as without redirecting.

    What I am doing, because Google Analytics will see "/awesome" AND "/my-awesome-page" as two diffferent URLs, it is a real pain to try to track all of our author's Alias Urls. This is why I take the URL Alias and then manually redirect and append my custom query string of

     "?vanityUrl=" + urlAlias

    so I can more accurately track page views if someone used the URL Alias a sa vanity URL.

    That said, is someone comes in with the URL

    Then I want it to redirect to my manually generated URL with my custom "?vanityurl=awesome" PLUS append the UTM code to it so we can track external campaigns to that same page. Because most likely my authors are going to make multiple campaigns to the same "vanity URL "(umbracoUrlAlias) using different campaigns.

    I don't quite know to do that, OR should I start by allowing the UTM and the strip off the UTM (which not sure how to do that quite yet), and then do the redirect to my custom URL after that.

  • Amir Khan 1276 posts 2712 karma points
    Jun 09, 2020 @ 22:06
    Amir Khan

    Let me try to set something up if I have time tonight. You also want to think about making sure the URLs are canonical as you'll technically end up with 3 urls for each page right?

Please Sign in or register to post replies

Write your reply to: