winter / wn-googleanalytics-plugin
Google Analytics integration plugin for Winter CMS
Fund package maintenance!
wintercms
Open Collective
Installs: 1 571
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 6
Forks: 6
Open Issues: 1
Type:winter-plugin
Requires
- php: >=7.2
- composer/installers: ~1.0
- google/apiclient: ^2.4
- guzzlehttp/guzzle: ~6.0|~7.0
- psr/cache: ^1.0
Replaces
README
This plugin adds Google Analytics tracking and reporting features to the Winter CMS.
Note: The v3.x series of this plugin only supports Google Analytics v4 (GA4) as GA3 / UA are being retired. If you wish to use this plugin for GA3 / UA, you must roll back to v2.0.2 of this plugin.
Supports:
- Multiple backend dashboard report widgets for GA4 data
- Frontend component to enable reporting analytics data to GA4
- Anonymize IP addresses
- Block tracking of authenticated backend users
- Block tracking while in maintenance mode
Installation
This plugin is available for installation via Composer.
composer require winter/wn-googleanalytics-plugin
After installing the plugin you will need to run the migrations and (if you are using a public folder) republish your public directory.
php artisan migrate
Configuration
Configure the API
To get started using Google Analytics API, you need to first create or select a project in the Google Developers Console and enable the API. Using this link guides you through the process and activates the Google Analytics API automatically.
Alternatively, you can activate the Google Analytics API yourself in the Developers Console by doing the following:
-
Open the Credentials page.
-
Select the Overview menu item and enable the Analytics API.
In either case, you should end up on the Credentials page and can create your project's credentials from here.
Create a client ID
-
Open the Service accounts section of the Developers Console's Permissions page.
-
Click Create service account.
-
In the Create service account window, type a name for the service account, eg:
Winter CMS Analytics
. -
Place a tick in the box Furnish a new private key and select JSON for the Key type.
-
Then, click Create.
-
A
.json
file will be generated, this is the private key for your account. Accept the download and save it to your computer. -
Click Close.
-
You should see an email ending with
iam.gserviceaccount.com
, if not select the Permissions tab. Copy this address to your clipboard.
Configure Google Analytics
-
In a new tab, navigate to the main Google Analytics site and select the property you want to track.
-
Click the Admin main menu tab at the top. Select the Property > User Management menu item.
-
Paste the email address from the clipboard in to the field Add permissions for and make sure the permission is set to Read & Analyse. Click Add.
-
Click the Admin main menu tab again and select View > View Settings from the menu. Copy to your clipboard the Profile ID (should be a number).
Configure Winter CMS back-end area
-
Open your Winter CMS back-end administration area and open Settings > Google Analytics.
-
Paste the Property ID from the clipboard in to the field Analytics Property ID number.
-
Upload the previously downloaded
.json
private key file to the Private key field. -
Specify the Tracking ID (eg
G-1234ABCDEF
) and Domain name values if you are going to use the plugin's built-in tracking component. To find this code, select Admin > Property > Property Settings from the Google Analytics menu. -
If Tracking is not working initially, enter the value
auto
for the Domain name field. This is a fix for newborn Google Analytics accounts.
Adding the tracking code
Using the tracking code provided by the plugin is optional - the reporting part does not depend on whether you track your traffic with the built-in tracker or with a custom code generated by Google Analytics tools.
To add the plugin's tracking code to your website just drop the Google Analytics Tracker component to your CMS layout and add this code to the layout code after the page tag:
{% page %}
{% component 'googleTracker' %}
Troubleshooting
Fix for Windows / XAMPP
cURL error 60: SSL certificate problem: unable to get local issuer certificate
-
Follow this link: http://curl.haxx.se/ca/cacert.pem and save it in a file called
cacert.pem
. -
Open your
php.ini
file insert or edit the following line:curl.cainfo = "[pathtothisfile]\cacert.pem"
-
Restart Apache
Popup message simply saying "error" when trying to add the JSON key file.
Some ad-blockers (such as Chrome's uBlock Origin extension or Firefox's own built-in algorithms) may prevent the uploading of the JSON file key. Disabling these, or whitelisting the Winter CMS website, may resolve this issue for you.