Analytics statistics on your site

    I remember once slipped a couple of entries ( here and here ) about importing data from Google Analytics to display summary charts on the site, in general, as Habré . After reading the above topics and not paying much attention then, I successfully "forgot" about them and went further. And now, there was a need to implement something similar on one of their projects.

    image

    Looking at the Habr again, I was surprised to find that then no one had found any solution to implement this task. Therefore, I decided to add a script to export data from GA and display it on the site. Actually, I want to share it with the public.

    UPDATE :
    Added one more version, details and a link under a cat.


    You can download from here .

    Take the new version here .
    The $ id variable has been added to config.php, 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 adjust the $ url variables in the previous one by specifying the required id in the query string.
    Thanks Orenlab for the comment.

    UPDATE 2:

    The next version is here .
    Www.askapache.com removed from ga.php from the request .
    Without this variable, everything works just as well. Why it was necessary remains to be guessed. I looked at Google headlines - I didn’t see anything like it.
    thanksAlexSpaizNet for the comment.

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

    The changes are as follows:
    - The parser is slightly rebuilt. Unfortunately, there were some difficulties when working with English installed in GA, therefore:
    ATTENTION! To work properly in Google Analytics settings necessarily have to be installed Russian language

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

    List of all version - here

    UPDATE 4:
    due to the recent changes in the GA came out Version 2.0b
    Pick up as usual from here

    To make it clear what was happening and what has changed, I will briefly write down 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 language used in reports. Now through the script I always get the English language, through the browser, normally picking up the report, I get the one that should be. What it is connected with until I figured it out.
    So, actually to the changes.

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

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

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

    3) since the date we get is already in a different form, format it as it doesn’t work out earlier, therefore, comment out the lines after
    // format the date

    4) well, actually in the English language, it is customary to separate the thousandth with a comma, which we remove in order to amcharts normally drew a chart

    // remove the thousands separator 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 (",", "

    It seems that everything ... we will deal with problems with the Russian in the near future.

    UPDATE 5:
    Version 2.0.1 has appeared.
    We take away.
    Now the Russian language works normally.
    Changes were made to the ga.php file, one cookie was added - $ aFoundCookies [1] [] = "AnalyticsUserLocale = ru";

    Well, in terms of date formatting, small changes have been made in stat.php (but this is not necessary ...) I

    will briefly describe the main nuances, settings and how it works.
    (installation guide below)

    So, probably everyone knows that it is possible 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, it is only possible to download the report after authorization, and no external links to download the report without authorization are not available. True, there is the possibility of sending a report to soap (I read that some exported data in this way, i.e. the report was sent to soap, then a script was run on the crown that took the mail, and the received report was parsed, but, IMHO, this is nonsense). In general, to receive it, we need to emulate the login into the system and receive data after that. To do this, we use the ga.php script (which, incidentally, was not written by me, but was found a few months ago in the wilds of the Serch forum, for which thanks to the developer, because it saved time on parsing google authentication).
    To accomplish the task, we would use either XML or CSV formats. I don’t know what pulled me at that moment, but I settled on the latter, i.e. This is a regular text file, which we need to parse in a certain way to import data and build graphs.

    The stat.php script parses the received data (it is the main executable script that loads ga.php into itself and, in short, generates result files on disk for creating diagrams)
    I tried to comment as much as possible on the code, so that it was clear what where and why, and it was possible to add the desired report without too much difficulty. I took, at my discretion, the 6 reports I needed: Attendance from the beginning of the existence of the resource (the date is specified in the settings), Attendance for the last three months, Referring sources, 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 display statistics, the google analytics code was installed only a few months ago, but I wanted to show the growth in traffic from the very beginning, i.e. for more than two years in this case. Therefore, the ability to "stick" to the data obtained from GA, information about the attendance for the time preceding the installation of the Google counter was added. the default is the static default.csv file, formed as necessary (the archive contains an example of all csv files, including this one). Where you get the data for him is up to you, in my case there were already 4 counters installed on the site, such as rambler, mail, etc. (whose testimonies, by the way, vary sooo substantially). If there is no need to use such a scheme, just specify the name of the variable indicating the name of the given file as false.

    AmCharts is used to display charts . This is a paid product, worth 85 euros for a single-site license. However, a free version is also available, which differs only in that the diagrams display an unobtrusive link to the developer's site.
    The settings for each of the diagrams are indicated in the corresponding xml files, which are well commented, and I think figuring out that there is no big deal to it. For presentation, I took a color scheme similar to the graphics on the Habr.
    Data for building diagrams are loaded from the csv files we generated above, specially prepared for use in amCharts.

    In the end, we get something like the following .

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

    0 * / 8 * * * / usr / local / bin / php /home/username/public_html/stats/stat.php I’ll be

    happy to hear comments and wishes!

    PS. Installation.
    Rename the file config.default.php to config.php. We
    edit it, specifying, as a minimum, the following settings:

    1. We take your account in Google Analytics
    id after login through the browser and go to the main page with reports:
    www.google.com / analytics / reporting /? reset = 1 & id = xxxxxxx & scid = xxxxxxx 2. Set the time zone if the server time does not match what you need. If it matches, just comment or delete these lines

    //учетная запись GA
    $u="user";
    $p="password";
    $id="1234567";



    //устанавливаем временную зону, во избежание недоразумений, если время на сервере отличается от необходимого
    putenv('TZ=Europe/Kiev');



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

    is the date starting from which you will draw the first chart, ie the beginning of all statistics

    Everything else can be left without touching, if you still want to - the other settings are commented on

    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 bunch of .csv files - this is the data for the graphs.
    To display the graphs, we request index.php

    In turn, stat.php can be thrust into crowns or any other task scheduler and run several times a day to automatically update schedules.

    Good luck! Thank you all for your interest!

    Attention! New version availablehere

    Also popular now: