Tools like Google Analytics are great for collecting data about your visitors, but what if you want to display dynamic content on your website? Maybe you need a Popular Articles section in your sidenav. On the contrary, maybe you want to promote the least popular pages. I’m sure those lonely pages would appreciate it. There are in fact several existing MODX Extras that set out to do this sort of thing, but Hits for MODX is different.
Hits is a lightweight extra that allows you to “punch” resources freely. Each punch stores a hit in a speedy custom database table. Just resources you ask? Hits is actually abstract and uses hit_key and hit_count parameters to keep track of how many times a given key has been punched. Idea being, you simple pass in your resource id as the key.
That was easy. Now that we are punching away, let’s move onto doing something useful with the data our site has been collecting. Your project manager walks over catches you hyper-blogging the GIF vs JIF debate and informs you that two hours ago you needed to add a new feature that displays links to the most popular Articles. Oh, you didn’t get that email did you? It needs to compare popularity across multiple Article Containers, never be more than 5 minutes out dated and not slow the site experience down at all. Before you take to the sea, breathe deep.
If only this was as simple as writing a getResources tag, you could get back to that all important GIF vs JIF debate and still have time to ride that fixed-gear bike home with enough time to torrent Mad Men before someone on your kickball team iMessages you the plot and ruins your whole week.
[[!getResources? &parents=`-1` &resources=`[[!Hits? &parents=`3,10,86` &limit=`6` &depth=`0` &outputSeparator=`,`]]` &tpl=`popularLink` &showHidden=`1` ]]
So above, we are telling getResources to ignore parents and instead include a defined list of resources. We use Hits to create a comma-delimited list of the six most viewed direct children of containers 3, 10, and 86. There’s no need to pass a tpl parameter into Hits in this case. By default, if a parents parameter is passed Hits will output each results hit_key (that’s the id of the resource you punched earlier).
I hope you’re still here, because…wait what the?! Calling getResources uncached on every page?! Now that’s not going to make our websites anyfaster. Remember what the project manager said? This is where getCache comes to save the day. Oh so you didn’t read my blogpost on how to Save your Cache? Ya, ok blame Google for killing Reader, it’s cool. getCache is an abstract MODX Extra that caches output to the filesystem. When cleverly used, it can share cached output across multiple pages. This is useful for when you have sections of your website that have the same output regardless of what page they are on. You know, like our sidebar.
[[!getCache? &element=`getResources` &cacheElement=`global-sidebar` &cacheElementKey=`popular-articles` &cacheExpires=`900` &parents=`-1` &resources=`[[!Hits? &parents=`3,10,86` &limit=`6` &depth=`0` &outputSeparator=`,`]]` &tpl=`popularLink` &showHidden=`1` ]]
Now we are only processing the getResources and the containing Hits tag once every 15 minutes rather than once every page.
If a resource ever is acting up, or just gets way to popular you can knock it out. This will clear hit_key 4 back to 0, then add a hit, resulting in a value of 1.
[[!Hits? &punch=`4` &knockout=`1` &amount=`1`]]
You can even steal hits.
[[!Hits? &punch=`4` &amount=`-9`]]
It’s “GIF” by the way.