1

I need to count webpage visits in Bolt CMS to make rating of pages. Something like in StackOverflow: enter image description here

I have contenttype pages with field visits. I know that I need to add in page template some code, but I don't understand how to increment field visits of my current record. Is it possible via twig?

Or I have to add some custom query in template? But I don't know how to do this.

I will be grateful for any advice.

T.O.M.
  • 83
  • 9

2 Answers2

1

Ok, there's a quick and potentially incorrect way to do this, within your twig template you can increment and save the visits value:

{% record.setValue('visits', record.visits +1 ) %} {% app.storage.saveContent(record) %}

This will be fine if you're working on a simple project but it does force a db query in every single pageview of your site (including from bots/automated scripts etc) so it isn't really ideal for a production medium/high traffic site.

If you want to tackle it in a more robust way then using the Google Analytics approach of an Async JS tracker is probably better, or you could even use the Google Analytics API to periodically update your visits based on the pageviews recorded by Google.

Ross Riley
  • 826
  • 5
  • 8
  • Great thanks! That's what I need! It's only for sorting, so I don't need complicated check. But I get some errors. When I paste your code in my template I get errors `Unknown "record" tag` and `Unknown "app" tag`. Where is the problem? – T.O.M. Feb 14 '18 at 10:23
0

there is a little more to this I guess. stack overflow also checks unique people that visited. But for a basic solution I would create an table in the database with a simple int field. and set it to 0. (for every page)

Then create a PHP file where you would get this.

$pagid (Get url)
$result = mysql_query('Select count from counter WHERE pageid=$pagid');
$newcount = $result['count'] + 1;

mysql_query('UPDATE counter SET count=$newcount');

Then simply include this in every page and test if when you refresh the page the counter goes up. (The thing I declared in my code field above is a general rundown of what needs to happen so youll need to tweak it a bit)

When you get this to work you can go and try to add some checker if visitor is unique etc. But that is a problem of its own.

If u are ok with using PHP then add the PHP tag the sessions tag and the sql tag, others may give you an better explanation than I did here.

Also the reason you would need to do this using PHP or other script is becouse twig the 'PHP' templated doesnt allow you to modify the database data. It only allows you to GET it.

Hope some of my explanation(s) made sense and hope this helps you with your problem!

B. Dionys
  • 916
  • 7
  • 34
  • Thanks for your reply. I understand how to organize a counter of page views. But I don't quite understand how to do this on Bolt CMS. To do this on PHP for me is not a problem, but I can not figure out how to call my PHP-script from the twig template. The solution I'm searching is needed for the Bolt CMS - this is an important point. – T.O.M. Feb 09 '18 at 16:38
  • 1
    well you need to find or create extension for it – Michał G Feb 10 '18 at 12:07