Copied to clipboard

Flag this post as spam?

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


  • Marcus Maunula 229 posts 386 karma points
    Oct 13, 2011 @ 16:23
    Marcus Maunula
    0

    Getting popular posts with Razor?

    Hi all,

    I know this has been asked before but I cannot really find a conclusive answer on this. I post it here because I need a solution i can use with Razor.

    Anyway. Why was these type of statistics omitted in the first place? Every other CMS seems to have some sort of reporting/statistics module you can use, since almost every newspaper etc features a most read, visited etc block. How are you supposed to do this without statistics? Google Analytics is fine but why should one depend on external API:s for this?

    Anyway. I found out that there is some sorrt of UmbracoStats or Reports module but no download could be found that supports the latest version. How about modifying the counters module to do this? It seems to not rely too much on database hits either.

    All suggestions that doesn't depend on Gooogle API are interesting.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 13, 2011 @ 17:08
    Dan Diplo
    0

    It simplest enought to write this. All you need to do is add a property to your master DocType called (say) "viewCount" that can store an integer (a TextBox would do) and then you can add a Macro (to your master template) that uses the Umbraco API to increment this value by 1 every page load. Not difficult.

    In fact, I did something like that once for a Project. At the time there was no Razor so I did it as an XSLT helper. The methods were:

           public static void UpdateViewCount(int id, int amount)
            {
                Document doc = new Document(id);
                if (doc == null)
                    return;
    
                Property viewCount = doc.getProperty("viewCount");
    
                if (viewCount != null)
                {
                    int count = 0;
    
                    if (viewCount.Value != null)
                    {
                        int.TryParse(viewCount.Value.ToString(), out count);
    
                    }
    
                    count += amount;
                    viewCount.Value = count;
    
                    try
                    {
                        doc.Save();
                    }
                    catch
                    { }
                }
            }
    
            public static void UpdateViewCount(int id)
            {
                UpdateViewCount(id, 1);
            }

     Easily adapted to a razor function.

    You can then use Razor or Node API to get values back.

    Oh, and I imagine the reason this isn't in the core of Umbraco is that Umbraco is a framework not a CMS as such. Having a heavy-weight stats module that performs a database hit for every page load is not something majority of people would want. Most clients want a proper analytics package and these are better provided by 3rd parties than wasting effort of core team adding it. Umbraco is flexible, so allows you to write your own easily however you want.

  • Marcus Maunula 229 posts 386 karma points
    Oct 13, 2011 @ 20:21
    Marcus Maunula
    0

    Thanks but also, not sure I agree with you about "most" people. These things feel like stuff that belongs in a core functionality to me. I can't think of one content site of note that doesn't use this functionality. I mean anything above the "joes homepage" stuff ;).

    Maybe this code snippet could be stickied somewhere? Feels like a good to have snippet.

     

     

     

Please Sign in or register to post replies

Write your reply to:

Draft