Pull data out of Google Analytics for pages and blog tags
- SilverStripe ^4
- [SilverStripe Blog ^3.2 (for tag traffic)] (https://github.com/silverstripe/silverstripe-blog)
- SilverStripe Queued Jobs ^4.2
The SilverStripe BlogAnalytics module uses a Google Service Account to interact with Google Analytics.
- Set up a service account here. You will receive a configuration such as this:
{ "type": "service_account", "project_id": "YOUR GOOGLE API PROJET ID", "private_key_id": "YOUR PRIVATE KEY ID", "private_key": "YOUR PRIVATE KEY", "client_email": "EMAIL ADDRESS FOR THE SERVICE ACCOUNT", "client_id": "CLIENT ID", "auth_uri": "AUTH URI", "token_uri": "TOKEN URI", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "CERT URL" }
- Add the email address for the account to Google Analytics
- Get a View ID for the Analytics view you wish to pull from
- base64 encode the configuration
- Add configuration to your environment:
GASERVICE="BASE64 ENCODED JSON CONFIGURATION FILE CONTENTS (to avoid escaping values in the private key)" GAVIEW="VIEW ID" GASTART="7daysago" GAEND="today" GAMINCUTOFF="100" # Don't track pages with fewer views than this
- Install with
composer require revstrat\bloganalytics
- Run
/dev/build?flush=all
. - Create a queued job of type RevStrat\BlogAnalytics\UpdateTrafficData and run it. The task will schedule itself on completion for 3 hours in the future
Examples:
TopTags, TopSiteTree, TopBlogPosts each return a list, with the target object itself under "Item", and the traffic count under "Traffic".
Each can take an argument to override the default result count limit.
<h3>Top Tags</h3>
<% loop TopTags(6) %>
$Item.Title, $Traffic<br />
<% end_loop %>
<% loop TopSiteTree %>
$Item.Title, $Traffic<br />
<% end_loop %>
<% loop TopBlogPosts %>
$Item.Title, $Traffic<br />
<% end_loop %>