Copied to clipboard

Flag this post as spam?

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


  • David Armitage 505 posts 2073 karma points
    Jan 05, 2020 @ 04:15
    David Armitage
    0

    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

  • Graham Davis 110 posts 376 karma points
    Jan 05, 2020 @ 04:41
    Graham Davis
    0

    Have you considered creating a static class that is executed in the master layout?

       @inherits UmbracoViewPage
    @{
       StaticClass.MyMethod(Model.Id, param1, param2)
    
    }
    <!DOCTYPE html>
    <html itemtype="http://schema.org/Organization" itemscope="">
    
  • David Armitage 505 posts 2073 karma points
    Jan 05, 2020 @ 05:03
    David Armitage
    0

    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.

  • Erik-Jan Westendorp 29 posts 295 karma points MVP 4x c-trib
    Jan 05, 2020 @ 09:14
    Erik-Jan Westendorp
    0

    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:

    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>();
            }
        }
    }
    

    See also the documentation at: https://our.umbraco.com/documentation/implementation/default-routing/Controller-Selection/

  • David Armitage 505 posts 2073 karma points
    Jan 05, 2020 @ 09:18
    David Armitage
    1

    Awesome. I will take a look at this now.

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Jan 05, 2020 @ 17:54
    Marc Goodson
    0

    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

  • David Armitage 505 posts 2073 karma points
    Jan 06, 2020 @ 13:21
    David Armitage
    0

    Great I will take a look at this.

    This sounds like exactly what I need.

Please Sign in or register to post replies

Write your reply to:

Draft