The URLRW rules turn the provided Url into one with appended parameters to identify specfic content. For example:
Vanity Url displayed in browser:
http://www.mywidgets.com/yellow-widgetsUrl after rewriting:
http://www.mywidgets.com/widgets.aspx?product=yellow-widgetsPicking up the parameters in .Net is easy:
HttpContext.Current.Request.QueryString["product"]
Things get a bit more complex when parameters need to be appended to the vanity Url and recognised by your code. PPC traffic to your yellow widgets might be sent to:
http://www.mywidgets.com/yellow-widgets?utm_source=google&utm_medium=ppc&utm_campaign=yellowwidgetsThis Url is re-written by your URLRW rules, and so you can't grab the utm_x parameters using Request.QueryString["utm_x"]. We can however access the 'unfriendly' Url using
HttpContext.Current.Request.RawUrlwhich using the above example will give us the string
"/yellow-widgets?utm_source=google&utm_medium=ppc&utm_campaign=yellowwidgets"but grabbing the parameter values with ease doesn't seem quite so obvious. We could write some kind of bespoke method to parse through the string to find our params and value. The good news is we don't have to. Using the following code you can take the 'real' or 'unfriendly' Url string, and grab the parameter values with ease:
Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);
string utm_source = HttpUtility.ParseQueryString(theRealURL.Query).Get("utm_source");
string utm_medium = HttpUtility.ParseQueryString(theRealURL.Query).Get("utm_medium");
string utm_campaign = HttpUtility.ParseQueryString(theRealURL.Query).Get("utm_campaign");
Cool. Very helpful
ReplyDelete