Copied to clipboard

Flag this post as spam?

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


  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Feb 15, 2010 @ 14:47
    Andy Butland
    0

    Most viewed page tracking

    Hi - I'm fairly new to Umbraco, and have a specific feature to implement - and hence hoped to run my ideas past the forum to see if those more expert here can confirm I'm on the right lines...

    I need to present a list of nodes ordered by "most viewed", and hence have set up the following:

     - a numeric field called "numberOfViews" on my document type

     - an XSLT macro the orders based on this field

    To update this field I've set up a user control on all pages that calls a custom stored procedure passing the nodeID, and recording a page view in a custom table.

    It's not critical that the ordering is completely up to date, and hence I've set up a test that checks every time the user control has loaded if an hour has passed - and if so I copy the summarised data to the node fields.

    I don't want to generate a new content version each time this update happens, and hence have coded a stored procedure to update the data directly in the database tables (updating the dataInt field in cmsPropertyData).

    At this point I presume the XML content cache isn't going to get updated as I haven't used the API - rather have updated the data in the tables directly.

    So - two questions please:

     - what's the best way to cause this XML content cache to be refreshed?

     - and generally, does it sound like I'm approaching this in the right way?  It seems that ideally I wouldn't be trying to update umbraco table data directly, but in this case for performance reasons I'm thinking it may be the best way,

    Thanks in advance for any assistance.

    Andy

  • dandrayne 1138 posts 2262 karma points
    Feb 15, 2010 @ 14:57
    dandrayne
    0

    If you want to just read directly from your new DB table you could try something like http://our.umbraco.org/projects/sql-for-xslt-%28jespercom%29, although best-practice would probably say to work out how to refresh the xml cache and grab the data from that.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 15:00
    Nik Wahlberg
    0

    Hi Andy,

    this is a bit tricky and I can see both positives and negatives with your description above. Perhaps there is a business requirements to store the numberOfViews in a field for viewing. However, I would approach this in the following way:

    1. Create a user control (much like you've done), called from the master template. All this UC does is record the documentID and increments the number of views (page loads) associated with the ID in an XML file. 

    2. Create an XSLT that reads this file and order the output by the most views and returns the top 10 "rows"

    This would accomplish your root requirements of displaying the most viewed pages without forcing you to dig into the Umb db tables or worrying about keeping content in sync etc. 

    Just my "2 cents" here...actually this would make a pretty cool package (hmmm)

    Thanks,
    Nik

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Feb 15, 2010 @ 15:43
    Andy Butland
    0

    Thanks both for your feedback - I'll look into your suggestions.

    Nik - I like that idea.  You're right that I don't actually need need to display numberOfViews anywhere - the only reason I have created this field is that seemed the easiest way to get the ordering in the XSLT done.

    One slight complication is that I need to track views by node AND date (the actual requirement is to show most viewed in variable period of time on the site).  But that's not necessarly a problem as I could stil generate the XML with the summary information as you suggest.

    However I'm still going to have a problem trying to link together this XML file (with nodeIds and number of views) and the Umbraco XML (with all the titles, dates and summary text that I need to display on the page.  Need to do an INNER JOIN (with my database hat on), order by the numberOfViews and take the top X... is that possible to do efficiently with XSLT?

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 16:12
    Nik Wahlberg
    0

    Actually, all you should need (either using XSLT or a .NET control) is the nodeID and pass that to NiceUrl umbraco.library.NiceUrl(nodeId) which will generate your URL and you can always query the XML cache for the title using XPath node [@id='nodeId']. Of course, you always have the option to go to the API Document d=new Document(nodeId) and grab the properties in a UC as well. Several ways to go here (most efficient being the XML cache as you're not making expensive DB trips). 

    Hope that makes sense. 

    Thanks,
    Nik

  • Bogdan 250 posts 427 karma points
    Feb 15, 2010 @ 16:17
    Bogdan
    0

    Why would you need to store the data in an xml file? You can make a multiline textbox property in all the nodes that you need to track, have a user control append the date every time the page is requested and make another user control that gets this data from all the nodes, sorts it etc and output whatever you may need. I'm also new to umbraco, I don't know how this would work with a large number of views.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 16:21
    Nik Wahlberg
    0

    bfi, that is another approach as well but now you're back to having to deal with DB updates and syncing of the document cache. The approach above would abstract the handling of this data and separate the business logic of tracking pages from maintenance of the content. If you wanted to show the pageviews as part of the document properties you could do that too by creating a read-only dataType. 

     

  • Bogdan 250 posts 427 karma points
    Feb 15, 2010 @ 16:53
    Bogdan
    0

    That's true Nik, but what I don't know is how resource intensive updating the db and refreshing the cache would be in this case, the coding part is pretty easy. Recently I made a user control that created nodes with Document.MakeNew and then UpdateDocumentCache(docId); The data was immediately available from the cache using nodeFactory and the db server cpu load wasn't very high for a few hundred connections at a time. Is there another reason except server resources for not using document properties?

  • Bogdan 250 posts 427 karma points
    Feb 15, 2010 @ 16:57
    Bogdan
    0

    Oh, and another thing that comes to mind is to try using google analytics and its api to achieve this.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Feb 15, 2010 @ 17:00
    Andy Butland
    0

    Thanks Nik - yes, it does.  I'll give that approach a go.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 17:24
    Nik Wahlberg
    0

    @BFI, you are very correct in that the document API is great for updating and creating pages. It seems, in this case (at least to me), that it shouldn't really be part of a documents properties but rather as external meta data about a particular document. I suppose one could make an argument either way. 

    @Andy, cool. Let us know how you make out. 

    Cheers,
    Nik

Please Sign in or register to post replies

Write your reply to:

Draft