Requirement: Statamic v2.11.4
- Rename the folder
GoogleAnalytics
and copy it to yoursite/addons
folder - Run
php please update:addons
to install the dependency.
Use the settings to configure your tracking id and other Google Analytics settings or create a settings file in site/settings/addons/google_analytics.yaml
tracking_id: UA-*******-*
async: false
beacon: false
anonymize_ip: false
display_features: false
link_id: false
track_uid: false
ignore_admins: false
debug: false
trace_debugging: false
disable_sending: false
Add the Google Analytics tag to your theme layout file just before your close </head>
tag.
{{ google_analytics }}
Optionally if you just want the Google Analytics Tracking ID to you can use the {{ google_analytics:tracking_id }}
tag.
Use Statamic's built-in environment specific settings to specify different settings for Google Analytics in development or testing.
To use the reporting features of the Google Analytics Addon you will have to create an account with Google, sign in and set it up with credentials to use Google's APIs.
-
Go to Google API’s site and select
Select a project
, and make a new project, or make sure the correct project is selected from the header. -
Enable the Google Analytics API by navigating to
Libary
in the left-hand side navigation. Search forGoogle Analytics API
, select this Libary to view the description and enable it. (Don't confuses this with theGoogle Analytics Reporting API
) -
Create Credentials by clicking on the
Create Credentials
button in the notification (on the top right) or by going to theCredentials
item in the left-hand side navigation. -
Select
Google Analytics API
from the dropdown for what API are you using? -
Select
Web Server ...
for where will you be calling the API from. -
Select
Application Data ...
for what data will you be accessing. -
Select
No, I'm not using them
for Are you using Google App Engine or Google Compute Engine? (unless you are) -
Click the button
What credentials do I need?
-
Enter a service account name (
statamic
for example) -
Select
Service Account > Service Account User
for the role -
Select
JSON
as the Key type -
Click
continue
, save the key file to a safe location, we will be using it later (and keep it private).
-
Go to the Google Analytics Website and navigate to the settings for the appropriate property.
-
Select
User Management
under the view column. -
Add a new user with
Read & Analyze
access using theclient_email
value from the JSON file you downloaded. -
Get the
View ID
by clicking onView Settings
on the Google Analytics website.
-
Add the
View ID
to the Google Analytics Settings under theReporting Configuration
section in the Statamic Control panel. -
Upload the JSON file & rename it to
site/settings/addons/google-analytics-credentials.json
or some other location outside of your version control system. Just make sure to set the path in the configuration (including the file name) if you put the file in a different location.
There are a number of widgets you can add to the Statamic Dashboard to quickly view reports, in addition to the Google Analytics report pages in the navigation.
The pipe |
below indicates options, only one of the options should be used.
-
type: GoogleAnalytics
width: half | full
dates: show | hide
-
type: GoogleAnalytics.MostVisited
width: half | full
dates: show | hide
-
type: GoogleAnalytics.Browsers
width: half | full
chart: doughnut | bar | horizontal-bar
dates: show | hide
-
type: GoogleAnalytics.Referrals
width: half | full
chart: doughnut | bar | horizontal-bar
dates: show | hide
To see page view stats on pages and entities add the Google Analytics field to your fieldset.
If you want to show how many hits a page has, you can use the page hits tag: {{ google_analytics:hits }}
optionally you can include period='30'
where 30 is the number of days you want to use for the page hits.
If you want to sort your collection entities by page hits you can do so using the custom sort filter and setting the default sort to none. {{ collection:blog filter="google_analytics" sort="false" }}
optionally you can include period='30'
where 30 is the number of days you want to use for the page hits.
If you are sorting or filtering content by page hits you may want to pre-cache your page hits numbers in the background. You can do this using the php please googleanalytics:precache-hits
command. If you are using period when you are using the page hits tag you should add --period=30
to the end of this command where 30 is the number of days you are using for your page hits sorting or filtering.
In order for this to be effective you should set up a cron job to run this command before your cache expires.
You can set the cache time for page hits on the settings page for Google Analytics.
By default, anybody with CP Access will be able to see Google Analytics reports.
If you want to restrict what users can see the Google Analytics reports you can the slugs of the roles you wish to grant access to on the settings page under Roles with Access
.
Example:
roles_with_access:
- admin
- blog-editor
- editor
- uBlock Origin, and possibly other addons of this type can cause the scripts this addon use to not load. If you disable the plugin for your domain the scripts should load, and the addon should work.