Umbraco 8 How to Hijack page load of all pages request
Hi Guys,
I have a requirement to hack into the page load of each page request as it loads to the front-end.
When a user visits a page I need to hack into the loading or loaded event so I can log some extra stats into a custom database table I have created.
I will need to access what page is been loaded.
Ideally I would want access to the node Id or guid. Failing that just access to the url that is been loaded would work.
I understand about Routing hijacking but this is not really what I want. I don't want to have to create a controller for each doc type plus.
I literally only want to hack into every page request in one place, collect information about what page has been loaded and log this in the database. All other routing and rendering should continue through its usual path.
I am just wondering if there is any page loading or event I can override, capture the page data, log my stuff to the database, then continue the user on their normal path?
If you are wondering why I want to do this then it's for a basic page stats content app I want to build. Something very very simple which does not rely on analytics. It's a cool feature one of my lager e-commerce projects will benefit from plus I thought it might sit nice as a ContentApp.
I plan to release this as a ContentApp so I really wanted my logic happening server side so I can compile this in a dll. I kind of want to save the user having to mess around with any extra front-end config if I can.
By default, Umbraco will execute every request via its built in default RenderMvcController. It is possible to change this behavior. You can create your own controller that inherit from the default RenderMvcController. In this controller you can override the index method and log the desired extras.
Use a Composer to change the default RenderMvcController to yours like so:
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Web;
using Umbraco.Web.Mvc;
namespace Umbraco8.Composers
{
public class SetDefaultRenderMvcControllerComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.SetDefaultRenderMvcController<CustomMvcController>();
}
}
}
Essentially 'just' before the page is rendered, the Prepared event is thrown, and you can handle the event to gain access to the content and information about the request that is about to be rendered... and perform your additional logging...
... but by writing to the database with this approach and the custom RenderMvcController event will slow down the time it takes to render each page from Umbraco...
so if it were possible to have an API controller that a bit of javascript pings once the page is loaded, then the additional logging won't impact the performance of the site... or perhaps the additional information could be logged via google analytics as a custom event.
Umbraco 8 How to Hijack page load of all pages request
Hi Guys,
I have a requirement to hack into the page load of each page request as it loads to the front-end.
When a user visits a page I need to hack into the loading or loaded event so I can log some extra stats into a custom database table I have created.
I will need to access what page is been loaded.
Ideally I would want access to the node Id or guid. Failing that just access to the url that is been loaded would work.
I understand about Routing hijacking but this is not really what I want. I don't want to have to create a controller for each doc type plus.
I literally only want to hack into every page request in one place, collect information about what page has been loaded and log this in the database. All other routing and rendering should continue through its usual path.
I am just wondering if there is any page loading or event I can override, capture the page data, log my stuff to the database, then continue the user on their normal path?
If you are wondering why I want to do this then it's for a basic page stats content app I want to build. Something very very simple which does not rely on analytics. It's a cool feature one of my lager e-commerce projects will benefit from plus I thought it might sit nice as a ContentApp.
Kind Regards
David
Have you considered creating a static class that is executed in the master layout?
Hi Graham,
Yes i have. That's my plan b.
I plan to release this as a ContentApp so I really wanted my logic happening server side so I can compile this in a dll. I kind of want to save the user having to mess around with any extra front-end config if I can.
Hi David,
By default, Umbraco will execute every request via its built in default RenderMvcController. It is possible to change this behavior. You can create your own controller that inherit from the default RenderMvcController. In this controller you can override the index method and log the desired extras.
Use a Composer to change the default RenderMvcController to yours like so:
See also the documentation at: https://our.umbraco.com/documentation/implementation/default-routing/Controller-Selection/
Awesome. I will take a look at this now.
Hi David
Other option to consider is handling the Prepared Event of the PublishedContentRequest:
https://our.umbraco.com/Documentation/Reference/Routing/Request-Pipeline/inbound-pipeline
Essentially 'just' before the page is rendered, the Prepared event is thrown, and you can handle the event to gain access to the content and information about the request that is about to be rendered... and perform your additional logging...
... but by writing to the database with this approach and the custom RenderMvcController event will slow down the time it takes to render each page from Umbraco...
so if it were possible to have an API controller that a bit of javascript pings once the page is loaded, then the additional logging won't impact the performance of the site... or perhaps the additional information could be logged via google analytics as a custom event.
regards
Marc
Great I will take a look at this.
This sounds like exactly what I need.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.