📜 ⬆️ ⬇️

Google Analytics statistics on your site

I remember how I skipped a couple of records ( here and here ) about importing data from Google Analytics to display summary diagrams on the site, in general, as in a habr . After reading the aforementioned topics and not paying particular attention then, I successfully “forgot” about them and went further on. And now, there is a need to implement something similar on one of their projects.

image

Having reviewed Habr again, I was surprised to find that then no one had found any solution for the implementation of this task. Therefore, I decided to scribble to export data from GA and display it on the site. Actually, I want to share it with the public.
')
UPDATE :
Added another version, details and link under the cut.


You can download from here .

New version to take here .
In the config.php added the variable $ id, which corresponds to the id in Google Analytics. This id is used in the URL of each report.
That is, you can either specify your id in the config in this version, or correct the $ url variables in the previous one by specifying the required id in the query string.
Thank you Orenlab for the comment.

UPDATE 2:

The next version is here .
In ga.php from request removed www.askapache.com
Without this variable, everything works just fine. Why it was necessary - it remains to be guessed. I looked through the Google headers - I did not see anything like that.
Thanks to AlexSpaizNet for the comment.

UPDATE 3:
Version 1.0 added
Take away from here
This is the first and last release of the script in the form and with the functions that are currently available. With it, you can easily adjust everything to fit your needs and use.

The changes are:
- Parser slightly rebuilt. Unfortunately, there were certain difficulties when working with the English established in GA, therefore:
ATTENTION! To work correctly, the Russian language must be installed in the Analytics settings.

- Added visiting schedules
- The stripos function has been replaced by strpos, since stripos does not work in recent versions of PHP
- Fixed problem with caching settings and data for graphs. Now, with each refresh, you should see the updated data
- index.html replaced by index.php
- the $ path variable appeared in the config.php - you must specify the full value for its value to the directory in which the files will be written
- also added reversing $ debug - if it is set to true, a log.txt file will be created, into which the stream will be dropped, all reports received from Google Analytics (i.e., the data that Google gave, even before we fired them)

List of all versions is here.

UPDATE 4:
In connection with the latest changes in GA version 2.0b was released
Take as usual from here

To make it clear what was happening and what has changed, I will briefly sign for the changes.

So, firstly Google changed the format of dates in reports, now instead of October 25, 2008 it looks like October 25, 2008
In addition, something has changed in the definition of the language used in the reports. Now through the script I always get the English language, through the browser, normally taking the report, I get the one that should be. What is the reason I have not figured it out yet.
So, actually to change.

1) It was decided to switch to another type of reports - TSV , as it turned out, under this mysterious name hides a text file with tab delimiters. This allowed us to not depend on the language at all.

so, in all variables $ url in stat.php fmt = 2 changed to fmt = 3

2) since now we have another split in text files - in the processings we change "," to "\ t"
these lines look something like
$ line = explode ("\ t", $ lines [$ i]);

3) since the date we get is already in a different form, it will not be possible to format it earlier, therefore, the comment lines after
// format the date

4) well, in fact, in the anl language, a thousandth part is taken to be separated by a comma, which we delete in order that amcharts normally draw a chart

// remove the thousands delimiter character
$ line [1] = str_replace (chr (194). chr (160), "", $ line [1]);
$ line [1] = str_replace (",", "", $ line [1]);
$ line [2] = str_replace (chr (194). chr (160), "", $ line [2]);
$ line [2] = str_replace (",", "", $ line [2]);

It seems everything ... with the problems with the Russian understand in the near future.

UPDATE 5:
Version 2.0.1 is available
Pick up
Now the Russian language works fine.
Changes are made to the ga.php file, one cookie has been added - $ aFoundCookies [1] [] = "AnalyticsUserLocale = en";

Well, on formatting dates, small changes are made in stat.php (but this is not necessary ...)

Briefly I will describe the main nuances, setting and principle of operation.
(installation guide below)

So, probably everyone knows that there is an opportunity to get a report from GA in one of the formats (PDF, XML, CSV and something else). Thus, this is the easiest way to export data. However, downloading a report is possible only after authorization, and no external links are available to download a report without authorization. True, it is possible to send a report to the soap (I read that some exported the data in this way, that is, the report was sent to the soap, then a script was run on the crown that took the mail, and the report was received, but, IMHO, this is nonsense). In general, in order to receive, we need to simulate a login to the system and obtain data after that. To do this, we use the ga.php script (which, by the way, was not written by me, but was found a few months ago in the wilds of the forum , since thanks to the developer, because it saved time to parse Google authentication).
For the implementation of the task we would be suitable formats either XML or CSV. I don’t know that at that moment I was torn, but I stopped at the last one, i.e. This is a plain text file, which we need to parse in a certain way for data import and plotting.

Parsing the data we have is a stat.php script (it is the main executable script that loads ga.php and, in short, generates results files on a disk for creating diagrams)
I tried to comment on the code as much as possible so that it was clear what was going where and why, and it was possible to add the necessary report without any particular difficulties. I took, at my discretion, the 6 reports I needed: Attendance since the beginning of the resource’s existence (the date is specified in the settings), Attendance for the last three months, Sources of transitions, Geography of visitors, browsers and OS used for the last 30 days.

All settings are specified in config.php

By the way, on the site for which I needed to make a display of statistics, google analytics code was installed only a few months ago, but I wanted to show an increase in attendance from the very beginning of existence, i.e more than two years in this case. Therefore, the possibility was added to “stick” to the data obtained from GA, information on attendance during the time prior to the installation of the Google counter. the default is the static default.csv file that was formed as necessary (there is an example of all csv files in the archive, including this one). Where to get the data for it is up to you, in my case as many as 4 counters like rambler, mail, etc. were installed on the site. (whose testimonies, by the way, vary sooo significantly). If there is no need to use such a scheme, simply specify the name of the variable indicating the name of this file as false.

AmCharts are closed for displaying diagrams. This is a paid product, worth 85 euros for a one-site license. However, a free version is also available, which differs only in that the diagrams display an unobtrusive link to the developer’s website.
The settings for each of the diagrams are indicated in the corresponding xml-files, which are well commented, and I think it’s hard to figure out what is in there. I took the color scheme for presentation, similar to the graphs on Habré.
The data for building charts is loaded from the above-generated csv files specially prepared for use in amCharts.

In the end, we get something like the following .

Now you can drive the script into crowns, and update the statistics several times a day, for example:

0 * / 8 * * * / usr / local / bin / php /home/username/public_html/stats/stat.php

I am pleased to hear comments and suggestions!

Ps. Installation
The file config.default.php is renamed to config.php
We edit it, specifying, as imaginary, the following settings:

1. Your Google Analytics account
id we take after login through the browser and go to the main page with reports:
www.google.com/analytics/reporting/?reset=1&id= xxxxxxx & scid = xxxxxxx

// GA
$u="user";
$p="password";
$id="1234567";


2. Set the time zone if the server time does not match what you need
// , ,
putenv('TZ=Europe/Kiev');

If it is the same, just comment or delete these lines.

3. Specify the starting date in the format YYYYMMDD
$datestart="20080101";

This is the date from which you will draw the first graph, i.e. beginning of all statistics

Everything else can not touch, if you still want - the rest of the settings are commented

Next, fill it all into the directory on the server
ATTENTION! You must have permissions to write to this directory from PHP

We request the stat.php file - it generates a handful of .csv files - this is the data for the graphs
To display the graphs, request index.php

In turn, stat.php can be thrown into cron or any other task scheduler and run once a day to automatically update schedules.

Good luck! Thank you all for your interest!

Attention! New version is available here.

Source: https://habr.com/ru/post/42080/


All Articles