Tracing User's Activities - How do I?

Topics: Writing modules
Nov 7, 2011 at 6:11 PM
Edited Nov 7, 2011 at 6:16 PM

I'm going to create a module for tracing and displaying user activity on a site, these information include joining date, writing/editing profile/blog/content actions, and much more things user can do.

This module will depend on the Profile module.

What are steps involved to do so? In other words, how to interact with the user data through code.

Thanks.

Coordinator
Nov 7, 2011 at 6:57 PM

A handler is probably the right place to intercept activity that broadly.

Nov 7, 2011 at 11:49 PM

Actually, I already wrote a Logging module that logs every time a user views a piece of content, it'd be easy to extend it for editing content and other activities; and it's nicely extensible so you can repurpose it for logging any kind of user activity. It also logs activity of unauthenticated users (using their IP address and host details to track unique locations). As Bertrand suggested, most of these events can be tracked using an IContentHandler.

I wanted to release it but it's a bit tricky, it has a dependency on a piece of private code that's slightly tricky to unravel. I'll have a look at doing so in the next few days, and if you want to help me develop that instead of writing your own from scratch I can put it up on Codeplex.

You don't necessarily need to depend on Profile. Profile will display a user page showing all parts on the User object. All you need to do is add a custom part to User, and if a site owner installs the Profile they will then see your part, but you can also have a separate admin section for the auditing information. In any case it should be optional whether the site owner wants all of a user's activities to be publicly visible (I'd have thought generally not; if anything only a summary should be there).

Nov 8, 2011 at 10:45 AM
Edited Nov 8, 2011 at 10:51 AM

Thanks randompete.

I want to see what you've done so far, so please publish your project.

Nov 8, 2011 at 11:47 AM

The module has a service (ILoggingService) to which you can make a call: CreateLog(string logType, Orchard.ContentManagement.IContent content, bool confirmed)

This creates a log record with a DateTime stamp, user Id, and IP and browser details. The logType string can be any unique token you like for recognising the log (e.g. "View" for content being viewed, "Edit" for editing etc.)

It returns a long id to identify the log record. This, and the "confirmed" field, are because I had a requirement when a user views a video that the log is only "completed" when the video actually starts playing (so if, for instance, the user doesn't have Flash installed, then we know they didn't actually get to watch the it). The module also provides a controller action which can be called to complete a log; this Url is passed down into my video playlist so the Flash player can perform the callback.

So as you can see, it's pretty flexible and handles a lot of logging situations. The only thing is, I haven't built any UI for viewing the logs, currently I'm manually querying the table in SQL server when I want to inspect the data! It'd be nice to have, on top of the Profile option, a detailed set of summaries and even graphs in the Dashboard, and maybe a way to generate email reports at daily or weekly intervals. We could also easily add a "View Count" part to content item displays.

I'm pretty busy today, but I might get chance tonight or tomorrow night to publish it. As well as separating the dependency I'll need to migrate our existing data to the new table structure as a result of the new project.

Coordinator
Nov 8, 2011 at 7:37 PM

Any chance this could integrate with Rules?

Nov 9, 2011 at 11:06 AM

Well, it's a good idea bertrandleroy, I never thought about using rules to trace events. I'll consider making the module available to the rules module later.

Nov 9, 2011 at 2:02 PM

I had a dig around in the Rules module. It could actually replace the need for a content handler, and just define audit events through the rules UI. We can have a "Log" action, and then use the existing content events to trigger it. Users can then customise these rules and add any new logging directly from the interface.

For triggering log summary emails, we could add a new timed event and a summary email action.

This would actually save us having to build any configuration UI, and add extra flexibility at the same time.

Nov 16, 2011 at 3:51 PM

It took a bit longer to get around to it than I thought but I've made some progress on this.

I've published the project on Codeplex: http://orchardaudit.codeplex.com/

Right now it features merely the API (IAuditService) but I'll start adding Rules integration next.

Nov 16, 2011 at 4:04 PM

Thanks randompete, I'm busy now, but I'll take a look at it as soon as possible.

Nov 17, 2011 at 7:31 PM

I've now added all the Rules engine support (including for content views), so the project is feature complete except for the admin reporting backend. Please let me know any feedback!

Coordinator
Nov 17, 2011 at 7:44 PM

Wow, you're fast. Thanks for all the great work. Keep it up!