Jump to content

count views of post


sakkoulas

Recommended Posts

What would you want to do with session data? I'm not sure what it stores for by default, but i'm pretty sure it does not keep track of/count visited pages for all users, including your anonymous visitors. So i don't see how you would put it to use for frontend-counting.

Is there any "auto" deletion of expired/old session data going on somewhere in PW?

If i remember correctly both the default filebased sessions as well as database sessions depend on/use PHP's built-in garbage collection. This means that roughly after a certain amount of time ($config->sessionExpireSeconds = 86400;), PHP starts cleaning up after itself, removing stale session data.

Link to comment
Share on other sites

good to know about the cleaning thank you :)

It starts a session for every visitor, so at the moment I think it's not worse than the counter discussed in this thread ?

It's tracking bots as well, but this could be filtered out..

what ever, maybe I'm wrong, but at the moment it's nice to know. at least for me :)

Link to comment
Share on other sites

  • 1 year later...

Something strange happening for me when I try to do this.

I am logged in as superuser however if I check what roles I have on the frontend it says I am just guest. Same happens if I log in as a custom role I have made.

It just says guest.

Any thoughts why this is?

I also can't view unpublished pages on the front end when logged in as superadmin so something seems to be up.

Thanks

Link to comment
Share on other sites

  • 1 year later...

I'm not sure if you'd actually save very much processing capabilities, as you'd still need to process one request per page (at least without CDN enabled). It's only faster by the time needed to render the actual page. I'd really suggest going with the right tool for the job, which is google analytics or something similar, if possible.

But the actual implementation would not be difficult / different to other ajax stuff often explained in the forums. Just send the current url to the server, look for the page in that path and count the view field up.

  • Like 2
Link to comment
Share on other sites

Trying to show popular pages on the site, hence the counting. but I guess I was wondering if you could cache everything but a segment of the php that did the tracking per request, or am i jumping up the wrong tree...?

Link to comment
Share on other sites

I call boxes of filter links here via ajax on demand: http://joerg-hempel.com/archiv/

In the ProCached file, the content is empty. On demand it fetches it via ajax request, and on PW side via a page and urlsegments.

You can use the network tab to view the request. But it is only requested one time per session. I store the fetched content in client browser local session storage, to avoid multiple server requests for the same content, if the user clicks around on the filter tabs.

  • Like 1
Link to comment
Share on other sites

I use piwik for a while now and love the extended statistics..

But you should be able to place a hidden image on the procached page which requests an uncached page of your installation which counts the hits that's whats piwik is doing in an <noscript> block for js disabled browsers

e.g.

<img class="hidden" src="/counter.php">
or
<img class="hidden" src="/pathOfUncachedPage">

 

  • Like 2
Link to comment
Share on other sites

13 hours ago, benbyf said:

anyone implemented a counter via AJAX or in another procache compatible way?

I think that is what this reply covers:

 

13 hours ago, LostKobrakai said:

I'd really suggest going with the right tool for the job, which is google analytics or something similar, if possible.

13 hours ago, benbyf said:

Trying to show popular pages on the site

GAPI looks like a nice interface for accessing Google Analytics stats.

  • Like 1
Link to comment
Share on other sites

2 hours ago, benbyf said:

like the idea of $cache, is there any good resources? not really sure how i should use it as I normaly simply use the template specific caching within PW's admin.

maybe 

 

  • Like 1
Link to comment
Share on other sites

I have done it for one site. Google Analytics does the tracking, then pulling top 10 posts in last 7 days through API. Not maintaining the site anymore, but I remember I used Wanzes module for the API talking. 

  • Like 1
Link to comment
Share on other sites

  • 7 months later...

Hello. Trying to implement Most visited pages on my recipe site. I already added a field called recipe_views_count and the code to count visits (for now not excluding the session):

    if (!$user->isSuperuser()) {
        $page->recipe_views_counter += 1;
        $page->of(false);
        $page->save('recipe_views_counter');
        $page->of(true);
    }

so all that part is working fine and I am able to see the counter incrementing. Where I am stuck is how to show the most viewed pages now and sort them by the number of visits (max. first)?

Trying this:

$result = $pages->find("template=recipe_inner, sort=-recipe_views_count, limit=4");
foreach($result as $most_views) {

echo $most_views->title;

}

However something is not right as the query returns empty. If I remove the sort criteria it shows the proper number but for sure not taking into consideration the views count.

Link to comment
Share on other sites

At first glance, it appears to be a typo...

...already added a field called recipe_views_count.

But your counter references $page->recipe_views_counter += 1.

And your pages->find references sort=-recipe_views_count

Link to comment
Share on other sites

Hello @rick and thank you for your reply.

You are perfectly right that it was a typo, however the query result is still empty:

$result = $pages->find("template=recipe_inner, sort=-recipe_views_counter, limit=4");
                foreach($result as $most_views) {

....

}

It seems like the sort selector is not picking up posts...

I am always forgetting about the miraculous Tracy Debugger so will check whit it what is wrong...

Link to comment
Share on other sites

Tried again this morning using get instead find:

$result = $pages->get("/recipes/")->child("sort=recipe_views_counter, limit=4");

The result was 26 recipes instead of the 4 limitted and I am not aware where it got 26 from as it is not the number of all available ones and is not even the views number. What am I missing here?

As far as I removed the sort it works just fine:

$result = $pages->find("template=recipes-inner, sort=-modified, limit=4");    

So it seems like I am unable to sort by the recipe_views_counter only. Could that be because of the field being an Integer?

Link to comment
Share on other sites

Ok, I got it working with the initial query:

$result = $pages->find("template=recipes-inner, sort=-recipe_views_counter, limit=4");

The issue was caused by the fact that I selected the recipe_views_counter to be Integer, but in Numeric Input Type I had Text by default. Changing it to Number (HTML5) did the trick.

One less thing to worry about ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...