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.




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.


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.1.8"

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

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.


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


 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.


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.

<?php global $googleanalyticsid, $googleanalyticspageview; ?>
<?php if (!empty($googleanalyticsid)): ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo $googleanalyticsid; ?>"></script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo $googleanalyticsid; ?>', {'page_path': '/<?php echo $googleanalyticspageview ? $googleanalyticspageview : $request_path; ?>'});
<?php endif; ?>


Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].