Integrating Mint with MODx
With AJAX driven applications all the rage, it’s no wonder that Mint is getting a lot of attention lately. Not only does it look good but, let’s face it, it works pretty darn good too! It’s easy to use and even easier to add-on to MODx. Read on to learn how…
Recently, I took a serious look at the various ways to track website statistics with MODx. Along with the typical log analysis applications like AWStats, Webalizer, and others, there’s also a lot of tag-based solutions that are implemented as either a third-party service or an application that you can download and install on your own server.
Free third-party services like Google Analytics and StatCounter are pretty good for what they are and very easy to implement…just insert a few lines into each page. However, there’s a downside to services like these. Because you don’t control the server the statistics are on if the server goes down so do your statistics!
PHP-based statistics applications provide the best of both worlds. Not only do you have full control over your stats but you can enhance the application as well. Two of the most publicized stat applications on MODx are SlimStats and, of course, Mint. Although SlimStats is a very straightforward and easy to use stats program, I prefer Mint mainly due to the highly extensible nature of the program. Mint has a pretty open API that allows developers to create plug-ins called ‘Peppers’ that allow for additional functionality. Already there are quite a few to choose from on the forums. This is one are that SlimStats needs to work on. Until then, I’ll stick with Mint. If you host just a single website with a few subdomains, the cost of Mint ($30 per domain) is justified. Otherwise, it might get a little costly for ya! In that case, you might want to stick with SlimStats for the time being.
Adding Mint to your MODx website is easy. To start, simply follow the instructions that come with Mint to install the application. It’s recommended that you install it to a newly created directory in the root of your site. I created a folder simply called ‘mint’ and installed it there. That way I can quickly get access to my Mint site simply by typing in www.mydomain.com/mint. Also, because of the rewrite rules MODx uses, it’s recommended that you place an htaccess file in your Mint directory and add a RewriteEngine Off directive in it. To make it easier, just copy the htaccess file in your manager directory since it already contains this directive.
After installing Mint, it’s now time to give MODx what it needs to communicate with Mint. First and foremost, ignore any instructions Mint gives you about altering any htaccess files…they’re not needed. Also, it’s recommended that you add any Peppers you want before you integrate Mint into MODx…it just makes it easier in the long run.
The first step to adding Mint into MODx is to create a small snippet that will insert the correct script line whenever needed. Log into the Manager and under Resources -> Manage Resources, go to the Snippets tab. Create a new snippet called MintStats and insert the following lines into the textbox:
if($this->isFrontEnd() && !isset($_SESSION['mgrValidated'])){
$script = '<script src="/mint/?js" type="text/javascript"></script>';
}
return $script;
So long as Mint was installed in a root directory called ‘mint’, you shouldn’t have to change anything. If you created a directory with a different name, be sure and change the directory name in the $script variable above.
What this snippet does is very simple. It checks to see if the parser is running in ‘FrontEnd’ mode and checks to see if the current user is logged into the Manager. If the first check is true and the second is false then the script will be inserted wherever the snippet call is made. The reason for this is that you don’t want to track any hits if a user is previewing a page from within the manager or is logged into the manager and using QuickEdit to edit pages.
Once you’ve added the snippet, now it’s just a matter of adding the snippet to each of the templates you want to add tracking to. Simply add the snippet call just before the ending </head> tag to each template. The snippet needs to be called so that it’s uncached, else it won’t be able to pick up on the session variables and such.
Now, log into your manager and preview a few pages in your site. Launch the site from the Manager and browse around some more. Now go back into Mint and check your stats. See anything? You shouldn’t! Nothing should be tracked if you’re logged into the manager. You can check this simply by browsing the source of a parsed page. The script that would be inserted into each page shouldn’t show up. However, if you log out of the manager and visit your site normally you’ll see that the script is inserted and, going back to Mint, that hits are recorded.
Mint may not be free…but it sure is a great application! Have fun!
Comments:
A really advanced solution particulary interesting.
A really advanced solution particulary interesting.
Thank you for this simple tutorial the intigration didn't help BUT your blog has helped me find the amazing Mint script Thank you...
Note if anyone wants to sell out and get the monster that is Twatch then think twise it takes over most of your script, slows down your server and my biggest pet-T is its an encripted script so you cant make any alterations.
<script src="mint/?js" type="text/javascript"></script>
Any advice?
You must be logged into the forums to comment. Please login