Jump to content

ProCache & page counter


Martijn Geerts
 Share

Recommended Posts

I just (like 5 mins ago) implemented "Most Viewed Pages" widget for a client. I used Wanzes excellent ProcessGoogleAnalytics module and it was easy enough pull data when it was first setup.


It might be that this is not a fit for your project Martijn (if you need the data for selectors for example), but this is how I did it:

		$pga = wire('modules')->get("ProcessGoogleAnalytics");
		$ga = new GoogleAnalyticsAPI();
		$ga->auth->setClientId($pga->clientId);
		$ga->auth->setClientSecret($pga->clientSecret);
		$ga->auth->setRedirectUri($pga->page->httpUrl);
		$ga->setAccessToken($pga->accessToken);
		$ga->setAccountId($pga->accountId);

		$defaults = array(
		    'start-date' => date('Y-m-d', strtotime('-1 month')),
		    'end-date' => date('Y-m-d'),
		);
		$ga->setDefaultQueryParams($defaults);

		$params = array(
		    'metrics' => 'ga:pageviews',
		    'dimensions' => 'ga:pagePath',
		    'sort' => '-ga:pageviews',
			'max-results' => 11,
		);
		$visits = $ga->query($params);

		$out = "<h2>Most read articles</h2><ul>";

		foreach($visits['rows'] as $row) {
			if ($row[0] == "/") continue; // Skip the homepage, should be on top 10 always, if not, then 11 articles is shown
			$p = $this->pages->get($row[0]);
			if ( ! $p->id ) continue; // For some reason page is not found anymore...
			$out .= "<li><a href='$p->url'>$p->title</a></li>";
		}

		$out .= "</ul>";
  • Like 8
Link to comment
Share on other sites

If using ProCache, it would be kind of defeating some of the benefit of it if you are still loading ProcessWire for every request to update a view counter. I'd think you might be better off using an external service like Google Analytics to keep track of this for you. It already does this if you are using it, but of course that's not data you can easily pull back in to sort pages, etc. (as far as I know)

  • Like 1
Link to comment
Share on other sites

I'd think you might be better off using an external service like Google Analytics to keep track of this for you. It already does this if you are using it, but of course that's not data you can easily pull back in to sort pages, etc. (as far as I know)

I'm sure you can, using their API https://developers.google.com/analytics/devguides/reporting/core/v3/

  • Like 3
Link to comment
Share on other sites

  • 6 months later...

This might be a dead topic, but I'm looking at doing an implementation using Google Analytics (GA) API to do that on a website that has caching (and don't want to create too much custom cache code that I'll have to maintain afterwards), using a cron job. 

I looked at the documentation, and tried a few things with GA's Query Explorer : http://ga-dev-tools.appspot.com/explorer/

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:YOUR_GA_ID
// Set your request metrics here 
&metrics=ga:visits,ga:pageviews
// Set a filter for your page, you can combine multiple paths into one request using commas (OR operator) and multiple pagePath values
&filters=ga:pagePath=~/event/x/,ga:pagePath=~/en/events/x/
// start-date cannot go before 2005-01-01 (GA launch date) and end-date should be set to today
&start-date=2005-01-01&end-date=2014-04-15

This will return json data that you can then insert into a field. All in all, it seems easy:

Setup a script that will parse all pages, get multiple values, create a GA Api query, get values, update database fields.

Note that there are limits and quotas to the API. For what I need it, it's not a problem, but you might need to reach support to get more info.

Hope this helps some of you.

  • Like 2
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
 Share

  • Recently Browsing   0 members

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