Handling Session_Start in an Orchard CMS module

416 views Asked by At

A little background:

We need to develop a custom Orchard module for a client that will catch the external URL referrer (if any) and store it in a session variable for later use, e.g. submitting a query for one of their products.

My naive solution was to suggest we record the URL referrer on Session_Start because it is a reliable way of knowing how the user got to our site. The problem is that the client does not want us touching the global.asax.cs file. It has to be done via a custom module. This is non-negotiable.

So my question is this: how can I reliably retrieve and store the UrlReferrer information when a new session starts using an Orchard module?

Or alternatively, is there some other way I can hook into the page lifecycle and maybe check whether or not the previous page was an external referrer?

My most important concern here is I need to know whether someone clicked on a sponsored link and I need to find that out in a module, not global.asax.cs. I am not dead set on any particular tracking method, as long as it is possible in the Orchard framework given the limitations imposed on me.

FYI: Orchard version is 1.8+

2

There are 2 answers

4
devqon On

What I would do, is create a custom module and in there a custom controller:

public class ReferrerController : Controller {
    public ActionResult Index(string referrer) {

        if (Session["Referrer"] != null) {
            // do nothing, already used as entry point in the current session
        } else {
            // handle referrer, probably also some timestamp or hash
            Session["Referrer"] = referrer; // save in session
        }
        return RedirectToRoute("~/"); // redirect to home
    }
}

Obviously, also create a route for this. Then from the external referrers, go to this route where the referrers will be handled. (http://example.com/referrer?referrer=somereferrer)

0
Bertrand Le Roy On

You can do this from a filter. I implemented this a while ago in my commerce module, to enable giving discounts or attributions for conversions from partner sites, typically. You can see the source code for my filter here: https://github.com/bleroy/Nwazet.Commerce/blob/master/Filters/ReferrerFilter.cs