We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I'm looking for away to track page views on a resource and output the hits (number of pageviews) in a data attribute. We've tried mulitple snippets (ie Hits and PageHits, etc) with no luck.

    Any suggestions?

    Use Case:
    We have a page using getResources to display case studies. We have a filter option (JS based) that needs to filter by "most read". So, we want to put the number of hits a resource has in a data-page-hits="" attribute on the gR template.
      Sal Baldovinos
      SEO & DIGITAL MARKETING

      ARIEL DIGITAL | Maximize Your Potential
      www.arieldigitalmarketing.com
    • The MODX.today site uses Hits for that, tho I did make a slightly customised version that handles logging the hits with a plugin rather than a snippet. We don't display the hit count on the site just yet, but do use it to feed our Most Read page at https://modx.today/most-read.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
      • Quote from: markh at Aug 06, 2015, 09:51 PM
        The MODX.today site uses Hits for that, tho I did make a slightly customised version that handles logging the hits with a plugin rather than a snippet. We don't display the hit count on the site just yet, but do use it to feed our Most Read page at https://modx.today/most-read.

        I think if we were doing a "static" page that displayed 'most read', this would work. The problem is we have one page (case studies) and need to filter all the results of that page. One of the filter / sort options is to view "most read" and it would refresh / rearrange the listing of items (being pulled in from getResources)
          Sal Baldovinos
          SEO & DIGITAL MARKETING

          ARIEL DIGITAL | Maximize Your Potential
          www.arieldigitalmarketing.com
        • The "hits" snippet should do what you need. You would put this one in your resource template to record the hit
          [[!Hits? &punch=`[[*id]]`]]

          then use another call in your getResources tpl with its other properties, like &hit_key=`[[+id]]` and its own tpl to show the number of hits with its [[+hit_count]] placeholder.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 50881
            • 2 Posts
            I'm about a week into working in MODX... so, first sorry for the ignorance and thanks greatly for the help!

            I've got the +hit_count included in a data attribute on the <li>
            data-page-hits="[[+hit_count]]"


            I setup a tpl called "tpl_hits" with the following getResources call:

            [[getResources? &resources=`[[!Hits? &hit_key=`[[*id]]` ]]` ]]


            It's not outputting anything, so I'm missing something that I hope is obvious to you. smiley
            • Here is the getResources call on my page:
              <ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4 small-only-text-center js-ajax-items" id="grid-list">
              [[!getResources?
                  &tpl=`tpl_case_study_module`
                  &includeTVs=`1` 
                  &processTVs=`1`
                  &tvPrefix=``
                  &showHidden=`1`
                  &limit=`0`
                  &sortby=`{"publishedon":"DESC","createdon":"DESC"}`
              ]]
              </ul>


              This is the tpl_case_study_module
              <li class="js-filter-grid-item" data-page-hits="[[+hit_count]] / [[+hitCount]]" data-tag-value="[[!TaggerGetTags? &resources=`[[+id]]` &rowTpl=`comma_separated_tags`]][[+tags_company]]" data-title="[[+alias]]" data-pub-date="[[*publishedon:strtotime:date=`%Y%m%d`]]" data-is-featured="[[+featured]]">
              	<img class="tooltip-img" src="[[+page_image]]" alt="[[+longtitle]]">
              	 ... removed code for readability 
              	<h4>[[+menutitle]]</h4>
              	<p>[[+introtext:ellipsis=`75`]]</p>
              	<a class="link" href="[[~[[+id]]]]">View the Case Study</a>
              </li>


              This is the html_footer chunk for all page where we've placed the Hits snippet
              [[!Hits? &punch=`[[*id]]`]]



              Sidebar: Scott Blair is working with me on this, so same project / questions smiley @markh is familiar too wink
                Sal Baldovinos
                SEO & DIGITAL MARKETING

                ARIEL DIGITAL | Maximize Your Potential
                www.arieldigitalmarketing.com
                • 46886
                • 1,154 Posts
                Hi, I am not technically proficient so pls take this comment with a grain of salt, but my friend has implemented a solution that might work well for you. We use hitspage to track hits and pdoResources to output content pages (resources) according to hits. It does not however *display* the number of hits, but just sorts the resources according to number of hits.

                I didn't do it myself so I will just put all the files here. I am going to assume that "hitsPageTpl" is the default tpl for hitspage, if needed I can go find it.

                We have two snippets, HitsPage and HPCount. HPCount as I remember is necessary but I never understood how it works.

                I don't think HitsPage snippet has been changed at all, but here it is in case it has been changed:

                $page =  $modx->getObject('modResource', $modx->resource->get('id'));
                $view = 0;
                // Check the option to record in TV
                if($saveTv == 'true') {
                    // get the required TV object by name (or id)
                    $tv = $modx->getObject('modTemplateVar',array('name'=>'HitsPage'));
                    if($tv) {
                        $tvs = $modx->getObject('modTemplateVarResource',array('tmplvarid'=>$tv->id, 'contentid'=>$page->get('id')));
                        if($tvs) {
                            $viewTv = $tvs->get('value');
                            $tvs->set('value',intval($viewTv) + 1);
                            if($tvs->save()) $view = $viewTv;
                        } else {
                            $tvn = $modx->newObject('modTemplateVarResource');
                            $tvn->set('tmplvarid',$tv->id);
                            $tvn->set('contentid',$page->get('id'));
                            $tvn->set('value',1);
                            if($tvn->save()) $view = 1;
                        }       
                    } 
                } else {
                    $view = $page->getProperty('hitts','hitspage',$view);
                }
                $page->setProperty('hitts',intval($view) + 1,'hitspage');
                $page->save();
                return $view;


                HPCount is real simple but as I remember very important:

                <?php
                $modx->hpCount = true;
                return '';


                And our pdoResource call in the template:

                [[!pdoResources? 
                                            &parents=`87`
                                            &limit=`8` 
                                            &sortbyTV=`HitsPage`
                                            &tvPrefix=``
                                            &tpl=`hitsPageTpl`
                                            &sortdirTV=`DESC`
                                            &sortbyTVType=`integer`
                                            &includeTVs=`1`
                                            &includeTVList=`21`
                                            ]]


                The call is in a <ul> and I applied styling through the containing <div>

                HTH, let me know if you need more info.

                EDIT: Ah I forgot the call to HitsPage in the template. Here it is:
                <div class="hits-page">Views:[[!HitsPage? &saveTv=`true`]]</div>
                • Quote from: nuan88 at Aug 07, 2015, 10:27 AM
                  Hi, I am not technically proficient so pls take this comment with a grain of salt, but my friend has implemented a solution that might work well for you. We use hitspage to track hits and pdoResources to output content pages (resources) according to hits. It does not however *display* the number of hits, but just sorts the resources according to number of hits.

                  The problem is we dont need the page to be sorted on load. If it were a static page with a getResource call sorted by most read, then yes. The reason we need to the total number of hits on a resource is we're going to use jQuery to sort based on the number.
                    Sal Baldovinos
                    SEO & DIGITAL MARKETING

                    ARIEL DIGITAL | Maximize Your Potential
                    www.arieldigitalmarketing.com
                    • 33962
                    • 13 Posts
                    Maybe HitsPage is the right solution for you. Create a TV for PageHits an use the HitsPage-PlugIn and Snippet to track the pageviews.
                    Sortable via getResources and it's possible to use the TV for the data-attributes.
                    		[[!getResources?
                    		&parents=`0`
                    		&resources=`-1`
                    		&showHidden=`1`			  
                    		&limit=`5` 
                    		&includeTVs=`1`
                    		&includeTVList=`HitsPage,articleImage`
                    		&processTVs=`1`
                    		&tpl=`mostViewed` 
                    		&tvPrefix=``			  
                    		&sortbyTV=`HitsPage`
                    		&sortbyTVType=`integer`
                    		]]
                    
                      • 46886
                      • 1,154 Posts
                      Ah sorry I wouldn't be able to help you convert it to your needs.

                      Any particular reason you are devoted to the jquery for sorting? As a non-developer I feel modx gives you so much control that you can get lost in the middle somewhere. Anything is possible, but not everything is easy to work out.

                      Of course the dev types probably would say that you *can* do things just the way you like, if you put enough time and effort into it, but I am more of a path-of-least-resistance type. More control doesn't promise the most elegant solution will turn up easily.

                      Quote from: mayhemchaos at Aug 07, 2015, 01:10 PM
                      The problem is we dont need the page to be sorted on load. If it were a static page with a getResource call sorted by most read, then yes. The reason we need to the total number of hits on a resource is we're going to use jQuery to sort based on the number.