Giter Site home page Giter Site logo

hits's Introduction

Hits

Hits is a MODX Extra tracks pagehits with a punch by counting MODX Revolution page view and stores them in a custom table.

With hits you can:

  • record page hits based on a provided hit_key (such as a resource id)
  • Returns the results of a hit query and use with getResources

Usage

Record a hit for the current resource.

[[!Hits? &punch=`[[*id]]`]]

Record a hit for resource 3.

[[!Hits? &punch=`3`]]

Record 20 hit for resource 4

[[!Hits? &punch=`4` &amount=`20`]]

Remove 4 hit from resource 5.

[[!Hits? &punch=`5` &amount=`-4`]]

Get a comma-delimited list of ids of the 10 most visited pages 10 levels down from the web context.

[[!Hits? &parents=`0` &depth=`10` &limit=`10` &outputSeparator=`,`]]

Get a comma-delimited list of ids of the 4 least visited pages that are children of resource 2 and use your own hitInfo chunk to render results.

[[!Hits? &parents=`2` limit=`4` &dir=`ASC`  &chunk=`hitInfo` &outputSeparator=`,`]]

Get the four most hit resources, discluding the first

	[[!Hits? &parents=`0` &limit=`4` &offset=`1` &outputSeparator=`,`]]

Knockout resource 3 then add 2 hits (knockout zeros value before adding punches)

	[[!Hits? &punch=`3` &amount=`2` &knockout=`1`]]

Output the number of hits for a current resource using a custom Chunk template.

	[[!Hits? $hit_keys=`[[*id]]` &tpl=`my-custom-chunk`]]

Available Properties

Name Description Default Value Added in version
punch If set, a hit_key to record one or more hits for. Usually a resource id. 1.0.0
amount The amount of hits to record for the punched hit_key. 1 1.0.0
parents Comma-delimited list of ids serving as parents to search for most visited resources within. If provided, results are returned. 1.0.0
hit_keys Comma-delimited list of ids serving as hit_keys to include. If provided, results are returned. 1.3.0
depth Integer value indicating depth to search for resources from each parent. First level of resources beneath parent is depth. 10 1.0.0
tpl hit_key, hit_count, and id (of hit) paramters will be passed into the provided chunk for each result. outputs hit_key 1.0.0
limit The amount of results to return. 5 1.0.0
sort Property to sort results on. Available options are hit_count, hit_key or id. hit_count 1.0.0
dir Direction to sort properties on. DESC 1.0.0
outputSeparator An optional string to separate each tpl instance. "\n" 1.0.0
toPlaceholder If set, will assign the result to this placeholder rather than outputting. 1.0.0
offset Optionally offset results 1.1.0
knockout Set to 1 to zero hit_count of punched record before incrementing by amount 1.2.0

With getResources

Hits can be used be used with getResources to list the most or least visited pages. This will pass a comma seperated list of ids of the 10 most visited pages according to Hits into getResources.

[[getResources?
&resources=`[[!Hits? &parents=`0` &depth=`10` &limit=`10` &outSeperator=`,`]]`
...
]]

Optimization

Recording Hits

Hits needs to be called uncached whenever it is punching hits. If you don't want the processing of a hit to affect page load time you can record your hits after page load using AJAX.

Displaying Statistics

When using with getResources remember that you can utilize getCache to cache the results to the filesystem for a determined period time as well as share the cache across multiple pages. If you are displaying a "Most Visited Pages" nav in your sidebar, the results are probably going to be the same across all or multiple pages. Thus, you can utilize the getCache cacheElementKey paramater to share the cache file across multiple (in this case all) resources. Move getResources tag to a 'getMostViewed' Chunk.

[[!getCache?
&element=`getMostViewed`
&cacheExpires=`900`
&cacheKey=`hits`
&cacheElementKey=`getMostViewed`
]]

The getMostViewed chunk will now only be processed every 15 minutes and load from a single shared cache. This means no matter how many visitors we have, we are only processing this output once every 15 minutes.

Alternatively, if results from getResources vary from page to page, you could wrap the Hits tag with getCache.

[[getResources?
&resources=`[[!getCache? &element=`mostHitsIDs` &cacheExpires=`900` &cacheKey=`hits` &cacheElementKey=`mostHitsIDs`]]`
...
]]

hits's People

Contributors

jpdevries avatar sepiariver avatar the-dunnock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

hits's Issues

MySQL Optimization?

Couple main things I want to make sure I did right:

if logged in as admin three hits counted

Hi there!

I would like to use this extra, but I found an issue I guess:

when I'm logged to the manager and I visit a page with [[!Hits? &punch=[[*id]]]], the database amount gets incremented by 3. When not logged in it seems to be counting correct.

I also tried to use a snippet called "isAdmin" which checks if the user has admin-status or not. In this snippet I wanted [[!Hits? &punch=[[*id]]]] only get fired, when the user is not an admin/logged in:

[[!isAdmin:notempty=1:default=2[[!Hits? &punch=[[*id]]]]]]

So if a logged in admin loads a page, I get a "1" at the top of each page, if not logged in a "2". This is when the [[!Hits? &punch=[[*id]]]] should only be getting fired. But it does get fired even when logged in as admin.

I treid both ways, with the isAdmin-snippet and without - in both cases the result is the same (admin user 3 hits, non-admin user 1 hit).

Is there a possibility to edit the snippet to not count the admin-hits?

Kind regards from Germany,
Marc

Hits returns 0 on page

It returns the value of '0' on everypage where [[!Hits? &punch=[[*id]]]] is placed.

Any idea what's wrong?

Request - Include pagetitle?

Hi JP, just implemented your Hits extra, nice and easy. How easy would it be to include pagetitle or alias in the extra?

Readme Typo

Output the number of hits for a current resource using a custom Chunk template.

[[!Hits? $hit_keys=`[[*id]]` &tpl=`my-custom-chunk`]]

Should read:

[[!Hits? &hit_keys=`[[*id]]` &tpl=`my-custom-chunk`]]

&hit_keys not $hit_keys

&offset

FR: &offset property to return x number of records from the first, based on &dir property.

So if DESC, and &offset=10 will return record with 10th highest number of hits, if it exists. If it doesn't exist it would return nothing, to keep it simple.

Knockout Feature

pass in a comma separated list of hit_keys (page ids) and either set them all back to 0 or remove them from the database completely

Trailing Output Sepeartor

Snippet call used: [[Hits?parents=12 &depth=2 &limit=1 &outputSeparator=, ]]

returns:

66,104,100,107,89,84,121,74,65,140,73,70,86,118,55,43,106,123,69,71,83,135,151,93,114,127,122,101,158,117,82,62,142,124,116,139,110,41,132,67,68,87,128,138,113,120,103,98,64,144,63,130,75,145,126,115,137,149,119,92,24,102,37,25,56,157,76,134,105,155,20,21,23,33,32,30,22,49,31,35,153,129,39,125,14,51,159,

note the trailing comma in the comma separated list...?

Hits fall off for Trending GetResources

Looking to use this to build a trending get resources listing, thinking the values need some sort of gravity.

Usecase:

University website has a list of resources for students. That listing will change with what is relevant to them based on the time of year. Registration vs transferring for instance.

Prevent Misuse?

So this needs to be called uncached, and perform actions on the database but it would be ๐Ÿ‘Ž to hold up page load time to record a hit (unless it really is extremely fast). So, thinking the docs should probably instruct using something like xFPC to do so over AJAX post page load

Hits and getResources returns nothing

Hi there!

I'm using the latest Hits version with getResources like this:

[[getResources?
&parents=`-1`
&resources=`[[!Hits? &parents=`0` &depth=`10` &limit=`10` &outSeperator=`,`]]`
&tpl=`sucheArtikel` 
&includeContent=`1`
&processTVs=`1`
&includeTVs=`1`
]]

But the output is empty?

When I use the Hits-call on it's own like this:

[[!Hits? &parents=0 &depth=10 &limit=10 &outputSeparator=,]]

I'm getting this output:

201,183,153,223,202,231,157,205

So normally the getResources-call should look like this:

[[getResources?
&parents=`-1`
&resources=`201,183,153,223,202,231,157,205`
&tpl=`sucheArtikel` 
&includeContent=`1`
&processTVs=`1`
&includeTVs=`1`
]]

This version outputs the correct resources.

But why doesn't the Hits-call work within getResources?

Any help will be appreciated!

Output mistype in your readme

Output the number of hits for a current resource using a custom Chunk template.

[[!Hits? $hit_keys=[[*id]] &tpl=my-custom-chunk]]

$hit_keys=[[*id]]
use & instead $

Dashboard Widget

A simple dashboard widget showing most viewed resources according to Hits.

I go back and forth on if this is necessary, because BigBrother does the same and much more.

&limit property

&limit property seems to not be working. Only reproducible on one site so far. Will test further.

Snippet call used: [[Hits?parents=12 &depth=2 &limit=1 &outputSeparator=, ]]

returns:

66,104,100,107,89,84,121,74,65,140,73,70,86,118,55,43,106,123,69,71,83,135,151,93,114,127,122,101,158,117,82,62,142,124,116,139,110,41,132,67,68,87,128,138,113,120,103,98,64,144,63,130,75,145,126,115,137,149,119,92,24,102,37,25,56,157,76,134,105,155,20,21,23,33,32,30,22,49,31,35,153,129,39,125,14,51,159,

Also note the trailing comma in the comma separated list...?

Filter by date

There does not seem to be any way to filter the hits results by date. It would be very useful to be able to filter and display results by, for example:

Most hit today
Most hit past 7 days
Most hit past month
etc...

I am using pdoResources to display a chunk of most hit resources and have tried adding a date filter with where condition, but it does not work.

[[pdoResources? &parents=`1` &limit=`5` &resources=`[[Hits? &parents=`1` &outputSeparator=`,`]]` &tpl=`tplHits`]] &where=`[{"publishedon:>=":"[[getDate? &offset=`-1 month`]]"}]

If I remove &resources, the where condition works as expected. Is there some other snippet that would filter the resources by date and then from that selection of resources, show the most hit?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.