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 240 posts 431 karma points
    Jun 09, 2020 @ 16:30
    Carlos Mosqueda
    0

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

    0

    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:

    www.example.com/Foo

    and I redirect it to the actual page of:

    www.example.com/this-is-my-base-page?alturl=Foo

    But a UTM page would come in as:

    www.example.com/Foo?utmsource=Bar&utmmedium=Char&utm_campaign=Eor

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

    www.example.com/this-is-my-base-page?alturl=Foo&utmsource=Bar&utmmedium=Char&utm_campaign=Eor

    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 1282 posts 2739 karma points
    Jun 09, 2020 @ 19:02
    Amir Khan
    0

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

  • Carlos Mosqueda 240 posts 431 karma points
    Jun 09, 2020 @ 19:26
    Carlos Mosqueda
    0

    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 www.example.com/my-awesome-page, also has an 'umbracoUrlAlias' of "awesome".

    So when Umbraco sees the URL "/awesome" it renders the same content as www.example.com/my-awesome-page 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

    www.example.com/awesome?utmsource=Bar&utmmedium=Char

    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 1282 posts 2739 karma points
    Jun 09, 2020 @ 22:06
    Amir Khan
    0

    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:

Draft