1
5

Traffic analytics

To activate the analysis of the traffic on a website by Google, you must enable the Analytics API, install the Google Client Library and configure the parameters $googleapiid, $googleanalyticsid, $googleanalyticsaccount and $googleanalyticskeyfile in config.inc.

If the parameters $googleanalyticsaccount and $googleanalyticskeyfile are defined, the page Management of an administrator of the website displays the Google Analytics logo.

Click on the image to access the page Traffic.

URL:

Options:

trendline

Enter a URL. Choose the start date and the end date. Press Draw.

The page is reloaded with the graphics and a button to download the image.

Check the option trendline to draw a line showing the trend.

If the end date is smaller than the start date, the dates are automatically reversed. The start date and the end date cannot have the same value.

If there is no data for the specified period, no graphics is drawn.

Configuration

Google Webmasters - https://www.google.com/webmasters

Go to the Search Console.

Create a first property for the site with the address www.*, e.g. www.sitename.net, then a second one without the www., e.g. sitename.net.

In the Site Settings for the two properties, check that Preferred domain is the one starting with www., e.g. www.sitename.net.

Google Analytics - https://analytics.google.com/analytics

Create an account. You can use the same account for several websites. Go to Admin.

Create a property for the site, e.g. sitename.

In Property Settings, enter the URL of the site. Choose Embedded mode.

Click on Adjust Search Console. Click on Edit. Associate the URL of the site, e.g. www.sitename.net, to the property. Click on Save.

Configure the parameter $googleanalyticsid in config.inc with the Tracking ID UA-*.

Google APIs - https://console.developers.google.com/apis

NOTE: Click on the menu at the top left in the banner with the 3 bars - the hamburger - to come back the home page of the API Manager.

Create a project. Give the name of the site to the project, e.g. sitename. Modify the identifier of the project, e.g. sitename-14102. Click on Display the advanced options. Choose the region of the site.

Go to All projects. Click on the new project. Click on Service accounts. Click on Create a service account. Select the role Viewer in Project.

Give a name to the account, e.g. sitename. Copy the part of the proposed address before iam.gserviceaccount.com before the @, e.g. sitename-14102@sitename-14102.iam.gserviceaccount.com.

Check the option Indicate a new private key. Select the key type P12. Press Create. Save the file *.p12 at the root of the site. Note that the key is protected by the password notasecret. You can delete the other accounts.

Configure the parameter $googleanalyticsaccount in config.inc with the ID of the service account.

Go to the Dashboard. No API is activated. Click on Activate API. In Libraries, click on Analytics API. Click on Activate.

Return to the home page of the API Manager.

Click on Identifiers. Select in Create identifiers the option API Key. Name the key, e.g. sitename. Don't set any restriction. Press Save.

Configure the parameter $googleapiid in config.inc with the value of the key.

Check in config.inc that the parameters $googleapiid, $googleanalyticsid, $googleanalyticsaccount and $googleanalyticskeyfile are configured.

Google Analytics - https://analytics.google.com/analytics

Go to User Management in the property.

Add an access permission by copying the ID of the service account registered in the parameter $googleanalyticsaccount, e.g. sitename-14102@sitename-14102.iam.gserviceaccount.com, with the privileges Read & Analyze. Press Add.

Google Webmasters - https://www.google.com/webmasters

Check that the address of the site is associated to the Google Analytics property.

Installation of the code from Google

Install Composer.

Create the file composer.json at the root of the site with the following contents:

{
  "require": {
    "google/apiclient": "1.0.*@beta"
  }
}

Run install:

$ composer install

All the code is in the directory vendor.

NOTE: Add the files composer.json and composer.lock to Git. Add the directory vendor in .gitignore.

IMPORTANT: Configure the PHP parameter upload_tmp_dir in the configuration file of the site:

<VirtualHost *:80>
	DocumentRoot /var/www/sitename.net
	...
	php_admin_value open_basedir /var/www/sitename.net
	php_admin_value upload_tmp_dir /var/www/sitename.net/tmp
</VirtualHost>

Replace sitename.net by the domain name of the site.

Make sure Apache is allowed to write in the folder tmp at the root of the site.

Options

If you set the variable $with_period to true in the function analytics in blocks/analytics.php, selecting the period is done differently.

Options:

 1 week  2 weeks  1 month  3 months  6 months  1 year

In this mode, the end date is always the current date. The selected interval determines the start date.

By modifying the views and the variable $available_periods and by reprogramming the analysis of the form, one can easily add or remove periods.

Code

The code in JavaScript by Google is added by the final view standard of the folder layouts in the tag <head> if $googleanalyticsid is not false.

  1. <?php global $googleanalyticsid; ?>
  2. <?php if (!empty($googleanalyticsid)): ?>
  3. <script type="text/javascript">
  4.   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  5.   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  6.   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  7.   })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  8.  
  9.   ga('create', '<?php echo $googleanalyticsid; ?>', '<?php echo $sitename; ?>');
  10.   ga('send', 'pageview', '/<?php echo $request_path; ?>');
  11. </script>
  12. <?php endif; ?>

Comments

To add a comment, click here.