{"id":34004993,"url":"https://github.com/chappy84/google-analytics-server-side","last_synced_at":"2025-12-30T06:00:06.007Z","repository":{"id":62501081,"uuid":"2043443","full_name":"chappy84/google-analytics-server-side","owner":"chappy84","description":"Server side Google Analytics tracking in PHP","archived":true,"fork":false,"pushed_at":"2023-07-03T16:55:15.000Z","size":4623,"stargazers_count":24,"open_issues_count":0,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-12-14T22:33:17.501Z","etag":null,"topics":["google-analytics","php"],"latest_commit_sha":null,"homepage":"http://chappy84.github.io/google-analytics-server-side/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chappy84.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-07-13T18:25:14.000Z","updated_at":"2023-07-03T16:59:01.000Z","dependencies_parsed_at":"2022-11-02T12:01:31.044Z","dependency_job_id":null,"html_url":"https://github.com/chappy84/google-analytics-server-side","commit_stats":null,"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"purl":"pkg:github/chappy84/google-analytics-server-side","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chappy84%2Fgoogle-analytics-server-side","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chappy84%2Fgoogle-analytics-server-side/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chappy84%2Fgoogle-analytics-server-side/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chappy84%2Fgoogle-analytics-server-side/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chappy84","download_url":"https://codeload.github.com/chappy84/google-analytics-server-side/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chappy84%2Fgoogle-analytics-server-side/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28124122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-30T02:00:05.476Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["google-analytics","php"],"created_at":"2025-12-13T10:25:49.128Z","updated_at":"2025-12-30T06:00:05.992Z","avatar_url":"https://github.com/chappy84.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[Google Analytics Server Side][1]\n============================\n\nArchived Repo\n-------------\n\nThis is an archived project, simply here for posterity. It is no longer supported or updated, and should be considered End of Life.  \nIf you wish to continue to develop this code yourself, we recommend you fork it.\n\nThe project was reverse engineered from the ga.js Universal Analytics code, which [Google has now marked as \"End Of Life\"](https://support.google.com/analytics/answer/11583528?hl=en). This package **WILL NOT** work with the latest version of Google Analytics (GA4 at time or writing).  \nWe recommend you follow Google's recommendation, and [migrate to GA4](https://support.google.com/analytics/answer/10759417?sjid=5599945154580947509-EU), at which point you can then use the [Google Analytics 4 Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4) to record analytics from the server side.  \nThere are various existing PHP implementations of this protocol, which we won't recommend one. If switching, do your research, and pick a good one, or be bold, and create a new one.\n\nAbout\n-----\n\nGoogle Analytics Server Side is an implementation of the [Google Analytics web tracking ECMAScript][2] in [PHP][3].  \nIt provides server side Google Analytics tracking with a small easy to use PHP 5.3+ package.  \nImplemented are the parts of the interface that would be available without [ECMAScript][6] in a \nbrowser to detect certain features such as screen resolution / colour, flash / java plugin version etc.\n\nCODE: `git clone git://github.com/chappy84/google-analytics-server-side.git`  \nHOME: \u003chttp://github.com/chappy84/google-analytics-server-side\u003e  \nBUGS: \u003chttp://github.com/chappy84/google-analytics-server-side/issues\u003e  \n\nGoogle Analytics was developed by [Google][4].  \nThis PHP adaptation is maintained by [Tom Chapman][5].  \n\n[1]: http://git.io/gass\n[2]: https://developers.google.com/analytics/devguides/collection/gajs/\n[3]: http://www.php.net/\n[4]: http://www.google.com/analytics\n[5]: http://tom-chapman.uk/\n[6]: http://en.wikipedia.org/wiki/ECMAScript\n\n[![Build Status](https://secure.travis-ci.org/chappy84/google-analytics-server-side.png?branch=master)](http://travis-ci.org/chappy84/google-analytics-server-side)\n[![Master Code Coverage Status](https://coveralls.io/repos/chappy84/google-analytics-server-side/badge.png?branch=master)](https://coveralls.io/r/chappy84/google-analytics-server-side)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/655a47b5-a324-487f-9b14-67da007b24d1/mini.png)](https://insight.sensiolabs.com/projects/655a47b5-a324-487f-9b14-67da007b24d1)\n[![Latest Stable Version](https://poser.pugx.org/chappy84/google-analytics-server-side/v/stable)](https://packagist.org/packages/chappy84/google-analytics-server-side)\n[![Total Downloads](https://poser.pugx.org/chappy84/google-analytics-server-side/downloads)](https://packagist.org/packages/chappy84/google-analytics-server-side)\n[![License](https://poser.pugx.org/chappy84/google-analytics-server-side/license)](https://packagist.org/packages/chappy84/google-analytics-server-side)\n\nInstallation\n------------\n\nThe package is available to install using [composer][7] from the [packagist][8] repository since\nv0.8.6-beta. Simply require [chappy84/google-analytics-server-side][9] and it'll be installed,\nchecking the requirements.\n\nAlternatively if you don't want to use composer, the package, without tests, can be included by \nusing the following:\n\n```php\nrequire_once '\u003cbase_dir\u003e' . DIRECTORY_SEPARATOR . 'Gass' . DIRECTORY_SEPARATOR . 'Bootstrap.php';\n```\n\nwhere `\u003cbase_dir\u003e` is the base directory of Google Analytics Server Side on your filesystem.\n\n[7]: http://getcomposer.org/\n[8]: https://packagist.org/\n[9]: https://packagist.org/packages/chappy84/google-analytics-server-side\n\nUsage\n-----\n\nGoogle Analytics Server Side can be used simply in the following manner:\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide;\n$gass-\u003esetAccount('UA-XXXXXXX-X')\n    -\u003etrackPageView();\n```\n\nThe class constructor accepts an optional associative array parameter of available\nconfiguration options. Basically if there's a public method to set the variable\nthen it can be passed as part of the array to the class.\n\ne.g.\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide;\n$gass-\u003esetAccount('UA-XXXXXXX-X')\n    -\u003esetBotInfo(true);\n```\n\ncould also be done like this:\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide(\n    array(\n        'account' =\u003e 'UA-XXXXXXX-X',\n        'botInfo' =\u003e true\n    )\n);\n```\n\nThese options can also be set individually by the method setOption,\nor in one go with the method `setOptions`\n\nMost of the [current basic methods][10] available in the `ga.js` tracking code have been\nimplemented.  \nThe methods implemented are:\n\n- `deleteCustomVar`\n- `getAccount`\n- `getVersion`\n- `getVisitorCustomVar`\n- `setAccount`\n- `setCustomVar`\n- `setSessionCookieTimeout`\n- `setVisitorCookieTimeout`\n- `trackPageview`\n\nThe methods not implemented yet are:\n\n- `getName`\n- `setSampleRate`\n- `setSiteSpeedSampleRate`\n\nExtra methods are also available for the information which would normally be\npre-determined in the javascript or http request object from the browser. The User Agent,\nServer Name, Remote Address, Document Path, Document Referer, Google Analytics Version,\nAccepted Language, Cookies and Search Engines are all set automatically without any method\ncalls being required by the developer. However, the following methods are available to set\nthese variables and should be called before the `trackPageView` / `trackEvent` method to save\nthe tracking information:\n\n- `setVersion`\n- `setAcceptLanguage`\n- `setUserAgent`\n- `setServerName`\n- `setRemoteAddress`\n- `setDocumentPath`\n- `setDocumentReferer`\n- `setCookies`\n\nOn top of this there are also set methods to alter the default values for the the page\ntitle and document character set.\nThese are available via the following methods:\n\n- `setPageTitle`\n- `setCharset`\n\nGet methods are also provided for all of the above.  \nAll methods but `get` methods allow chaining for ease of use.\n\n### Event Tracking\n\nEvent tracking is implemented using the [same functionality as in the ga.js tracking code][11]\n\n```php\n\\Gass\\GoogleAnalyticsServerSide::trackEvent(\n     string $category, \n     string $action, \n    [string $label = null, \n    [int    $value = null, \n    [bool   $nonInteraction = false]]] \n);\n```\n\nN.B. `trackEvent()` does not require `trackPageView()` to be called first.  \nHowever if you do not call `trackPageView` first or set `nonInteraction` to `true` then your \npages/visit metric may become less than 1.\n\n[10]: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiBasicConfiguration\n[11]: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEventTracking\n\nBotInfo\n-------\n\nYou must enable `BotInfo` for it to ignore any search/trawler bots.  \nTo do this you need to pass one of `true`, and associative array, or an instance of the \nadapter you want to use into the class.  The code will default to the `BrowsCap` adapter. \nSetting this to true will use the default. If you pass an associative array, this will be \npassed to `BotInfo` and through to the Adapter. When providing an associative array you can \nalso pass the element `'adapter'` which will tell `BotInfo` which class to use as the adapter. \nYou can also pass an instance of a `Gass\\BotInfo\\BotInfoInterface` adapter which will be used by the \n`Gass\\BotInfo` class.\n\n### Adapters\n\nThere are two adapters available in the GASS package\n\n#### BrowsCap\nThere are five options as part of the array configuration parameter:\n\n- `\\Gass\\BotInfo\\BrowsCap::OPT_SAVE_PATH`: The Path where the ini file and latest version file are stored.\n- `\\Gass\\BotInfo\\BrowsCap::OPT_INI_FILE`: The name of the ini file to store the browscap ini data in.\n- `\\Gass\\BotInfo\\BrowsCap::OPT_LATEST_VERSION_DATE_FILE`: The name of the text file to store the latest version timestamp in.\n- `\\Gass\\BotInfo\\BrowsCap::OPT_BROWSCAP`: This is the same as the php ini setting [browscap][12], a file-system location where the [full_php_browscap.ini file][13] is located / can be downloaded to. \n- `\\Gass\\BotInfo\\BrowsCap::OPT_DISABLE_AUTO_UPDATE`: This disables the auto-update feature for the browscap.ini file. See the [update](#updating-the-ini-file) section below for further information.\n\nN/B: `OPT_BROWSCAP` will be ignored if you have set either `OPT_SAVE_PATH` or `OPT_INI_FILE`. `OPT_SAVE_PATH` or `OPT_INI_FILE` will also override any value derived from `OPT_BROWSCAP`. This is as `OPT_BROWSCAP` is intended as a fallback for the browscap ini setting, and backwards compatibility with previous versions of this package.  \n\ne.g.\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide(\n    array(\n        'botInfo' =\u003e true,\n        'account' =\u003e 'UA-XXXXXXX-X',\n    )\n);\n```\n\nor\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide(\n    array(\n        'botInfo' =\u003e array(\n            'adapter' =\u003e 'BrowsCap',\n            \\Gass\\BotInfo\\BrowsCap::OPT_SAVE_PATH =\u003e '/var/lib/browscap',\n            \\Gass\\BotInfo\\BrowsCap::OPT_INI_FILE =\u003e 'full_php_browscap.ini',\n        ),\n        'account' =\u003e 'UA-XXXXXXX-X'\n    )\n);\n```\n\nor\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide(array('account' =\u003e 'UA-XXXXXXX-X'));\n$browsCapAdapter = new \\Gass\\BotInfo\\BrowsCap;\n$gass-\u003esetBotInfo($browsCapAdapter);\n```\n##### Updating the ini file\n\nWhen an update for the browscap ini file is available [on the server][13] the code will \nautomatically download the file into the location provided.  \nThis functionality can be disabled by setting the `\\Gass\\BotInfo\\BrowsCap::OPT_DISABLE_AUTO_UPDATE` configuration option to `true`.  \n\nWith this disabled, this package provides another method to allow you to update the ini file automatically.  \nThe script `bin/gass-browscap-updater` can be setup to run with a scheduler. \nThis uses the same code from the auto-update feature, checking the cache file, and the latest update date stored on [browscap.org][13], then downloads a new version if one is available.\n\nThs script has the following command line options:\n\n- `-s` / `--save-path` The path to save the ini file and latest update cache file in\n- `-f` / `--ini-filename` The filename given to the browscap file downloaded\n- `-c` / `--cache-filename`: The filename given to the latest version date cache file\n- `-v` / `--version`: Output's the current version\n- `-h` / `--help`: Displays the help\n\nSee the help provided by the script for further information on usage.\n\n##### Notes\n\n* You MUST either provide location info for the browscap ini file or have the browscap ini setting \nset in php.ini, otherwise this adapter will not work.\n* Due to an issue with the browscap ini file only being loaded when PHP starts up \n(which is with the web-server apache, PHP-FPM etc.) the code deals with the ini file \nitself, rather than using the built in get_browser function. This ensures the auto-update \nfunctionality will work without the need to restart the web-server.\n\n#### UserAgentStringInfo\n\n***DEPRECATED*** - until udger.com (or a comparable service) implements csvs (or another data source) \nto replace user agent string info's csv, as user-agent-string.info has now shut down\n\nThis downloaded a csv list of search engine crawlers from [user-agent-string.info][14].  \nThere are three options as part of the array configuration parameter:\n\n- `\\Gass\\BotInfo\\UserAgentStringInfo::OPT_CACHE_PATH`: where to save the list of bots downloaded from user-agent-string.info (required)\n- `\\Gass\\BotInfo\\UserAgentStringInfo::OPT_CACHE_FILENAME`: the filename to save the list of bots to (optional, defaults to bots.csv)\n- `\\Gass\\BotInfo\\UserAgentStringInfo::OPT_CACHE_LIFETIME`: number of secods before the cache expires (optional, defaults to 2592000 (30 days))\n\nThis can be implemented in the same way as the BrowsCap adapter.\n\n[12]: http://www.php.net/manual/en/misc.configuration.php#ini.browscap\n[13]: http://browscap.org/ \n[14]: http://user-agent-string.info/download\n\nHttp\n----\n\nThis is a singleton class which provides http functionality across all sections of the\nGASS package.  \nThis will default to using the `Curl` adapter if the php curl extension is available, otherwise it'll \nfall back to the `Stream` adapter. It requires no options. All options should be passed as a \nconfiguration option to `GoogleAnalyticsServerSide`, either via the configuration parameter \nin the `'http'` element or via the `setHttp` method's parameter. This can either be an associative \narray or an instance of the required adapter.\n\ne.g.\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide(\n    array(\n        'account' =\u003e 'UA-XXXXXXX-X',\n        'http' =\u003e array(\n            'adapter' =\u003e 'Curl',\n            CURLOPT_PROXY =\u003e 'http://exampleproxy.local:8080'\n        )\n    )\n);\n```\n\nor\n\n```php\n$gass = new \\Gass\\GoogleAnalyticsServerSide(array('account' =\u003e 'UA-XXXXXXX-X'));\n$httpAdapter = new \\Gass\\Http\\Stream;\n$gass-\u003esetHttp($httpAdapter);\n```\n\n### Adapters\n\nThere are two Adapters available in `Gass\\Http`, these are:\n\n#### Stream\n`Stream` creates a stream context and utilises this stream with `file_get_contents`. See \n[php's example][15]. Any [available options][16] provided to this class will go into the \n`'http'` array for the stream context, thus you may pass any headers or proxy information etc. \ninto this to use in the connection when made.\n\n#### Curl\nThis utilises the php extension cURL. cURL is recommended, however as it's not always \navailable the code falls back to stream to allow all servers make http requests in the \ncorrect way.  \nAny options provided to this class must be passed using the [curl constants][17] as \nidentifiers (associative array keys or option names).\n\n[15]: http://www.php.net/manual/en/function.file-get-contents.php#refsect1-function.file-get-contents-examples\n[16]: https://secure.php.net/manual/en/context.http.php\n[17]: http://www.php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-parameters\n\nEnd User Location\n-----------------\n\nThe End User's Location will be reported as the location of the server if you use the GA Account \nnumber in the format `UA-XXXXXXX-X` as provided by Google. If you alter this to the format \n`MO-XXXXXXX-X` then the location will be tracked correctly and appear on the location map as \nit does with the normal ECMAScript tracking.\n\nCookies\n-------\n\nCookies are automatically set when either `trackPageView` or `trackEvent` are called.  \nThey are however only sent as headers to the browser once, thus if you call either \nfunction more than once, or call both functions, then they will only be included in the \nheaders when the first call is made.\n\nYou do have the option to turn off the sending of the cookie headers to the browser which \ncan be done by calling `disableCookieHeaders` before calling trackPageView / trackEvent for \nthe first time.\n\nTest Suite \u0026 CI\n---------------\n\nThis package uses [PHPUnit][20], along with [TravisCI][21], to test functionality on the\nsupported PHP minor versions 5.3, 5.4, 5.5, 5.6, 7.0, and 7.1, with unofficial support for 7.2, 7.3, and 7.4.\nThis is done by default on the latest bug fix point release of that minor point version to ensure it works.\n\nIf you're submitting a pull request, please ensure you've run the test suite with PHPUnit, installed via \n[composer][7]. Please see the instructions [here][18] on how to install it. After which you can [install][19] \nphpunit, and the  other required dev dependencies using `composer install`.\n\n[18]: https://getcomposer.org/doc/00-intro.md#downloading-the-composer-executable\n[19]: https://getcomposer.org/doc/03-cli.md#install\n[20]: https://github.com/sebastianbergmann/phpunit\n[21]: https://travis-ci.org/\n\nPHP Version\n-----------\n\nThe minimum supported version is PHP 5.3.23\n\n#### Un-supported versions of PHP\n\nI've left the following branches of versions which worked with the now un-supported versions of PHP:\n\n- [PHP 5.2 Branch][22] \n\nPlease feel free to use, fork etc. any of these branches. Any issues which arise in them won't \nhave fixes attempted I'm afraid. However if you've attempted a fix yourself, please lodge a \npull-request and It'll be considered. \n\n[22]: https://github.com/chappy84/google-analytics-server-side/tree/php-5.2\n\nLICENSE\n-------\n\nThis software uses the BSD 3-Clause license:\n\nCopyright (c) 2011-2020, Tom Chapman (http://tom-chapman.uk)\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n1. Redistributions of source code must retain the above copyright notice, this list of conditions\nand the following disclaimer.\n\n2. Redistributions in binary form must reproduce the above copyright notice, this list of\nconditions and the following disclaimer in the documentation and/or other materials provided with\nthe distribution.\n\n3. Neither the name of the copyright holder nor the names of its contributors may be used to\nendorse or promote products derived from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR\nIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY\nAND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR\nCONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\nWHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY\nWAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\n\nN/B: This code is nether written or endorsed by Google or any of it's employees.  \n\"Google\" and \"Google Analytics\" are trademarks of Google Inc. and it's respective subsidiaries.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchappy84%2Fgoogle-analytics-server-side","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchappy84%2Fgoogle-analytics-server-side","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchappy84%2Fgoogle-analytics-server-side/lists"}