├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src └── AnalyticsEvent.php └── tests ├── TestCase.php └── Unit └── SanityTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/osx,composer,phpstorm 3 | 4 | ### Composer ### 5 | composer.phar 6 | composer.lock 7 | /vendor/ 8 | 9 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 10 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 11 | # composer.lock 12 | 13 | ### OSX ### 14 | *.DS_Store 15 | .AppleDouble 16 | .LSOverride 17 | 18 | # Icon must end with two \r 19 | Icon 20 | 21 | # Thumbnails 22 | ._* 23 | 24 | # Files that might appear in the root of a volume 25 | .DocumentRevisions-V100 26 | .fseventsd 27 | .Spotlight-V100 28 | .TemporaryItems 29 | .Trashes 30 | .VolumeIcon.icns 31 | .com.apple.timemachine.donotpresent 32 | 33 | # Directories potentially created on remote AFP share 34 | .AppleDB 35 | .AppleDesktop 36 | Network Trash Folder 37 | Temporary Items 38 | .apdisk 39 | 40 | ### PhpStorm ### 41 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 42 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 43 | 44 | .idea 45 | 46 | # CMake 47 | cmake-build-debug/ 48 | 49 | # Mongo Explorer plugin: 50 | .idea/**/mongoSettings.xml 51 | 52 | ## File-based project format: 53 | *.iws 54 | 55 | ## Plugin-specific files: 56 | 57 | # IntelliJ 58 | /out/ 59 | 60 | # mpeltonen/sbt-idea plugin 61 | .idea_modules/ 62 | 63 | # JIRA plugin 64 | atlassian-ide-plugin.xml 65 | 66 | # Ruby plugin and RubyMine 67 | /.rakeTasks 68 | 69 | # Crashlytics plugin (for Android Studio and IntelliJ) 70 | com_crashlytics_export_strings.xml 71 | crashlytics.properties 72 | crashlytics-build.properties 73 | fabric.properties 74 | 75 | ### PhpStorm Patch ### 76 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 77 | 78 | # *.iml 79 | # modules.xml 80 | # .idea/misc.xml 81 | # *.ipr 82 | 83 | # Sonarlint plugin 84 | .idea/sonarlint 85 | 86 | # End of https://www.gitignore.io/api/osx,composer,phpstorm 87 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | matrix: 4 | fast_finish: true 5 | include: 6 | - php: 7.0 7 | - php: 7.1 8 | - php: 7.2 9 | 10 | allow_failures: 11 | - php: 7.2 12 | 13 | cache: 14 | directories: 15 | - vendor 16 | - $HOME/.composer/cache 17 | 18 | install: 19 | # show versions and env information 20 | - php --version 21 | - composer --version 22 | 23 | # disable xdebug for performance reasons 24 | - phpenv config-rm xdebug.ini || echo "xdebug is not installed" 25 | 26 | # Install out dependencies 27 | - travis_retry composer self-update 28 | - travis_retry composer install --prefer-source --no-interaction --dev 29 | 30 | before_script: 31 | # Enable code coverage 32 | - | 33 | if [ $TASK_TESTS_COVERAGE == 1 ]; then 34 | PHPUNIT_FLAGS="--coverage-clover=$TRAVIS_BUILD_DIR/build/logs/clover.xml" 35 | fi 36 | 37 | script: 38 | - vendor/bin/phpunit --verbose $PHPUNIT_FLAGS 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Craig Davis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP Analytics Events [![Build Status](https://travis-ci.org/there4/php-analytics-event.svg?branch=master)](https://travis-ci.org/there4/php-analytics-event) 2 | ================================================================================ 3 | > Create a Google Analytics Event from PHP 4 | 5 | This is a small class to post Analytics events from PHP. This is useful for 6 | logging and event tracking. 7 | 8 | ## Installation 9 | 10 | ```bash 11 | composer require there4/php-analytics-event 12 | ``` 13 | 14 | ## Example 15 | ```php 16 | trackEvent('resources', 'download', 'cli-latest'); 23 | ``` 24 | 25 | ## Related Projects 26 | 27 | The [Google Analytics Measurement Protocol library for PHP](https://github.com/theiconic/php-ga-measurement-protocol) project is a much larger implementation of the [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1). If you need more than a simple tracking event you should check it out. 28 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "there4/php-analytics-event", 3 | "description": "Send Google Analytics events from PHP", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Craig Davis", 8 | "email": "craig@there4development.com" 9 | } 10 | ], 11 | "keywords": ["analytics", "Google Analytics", "events"], 12 | "minimum-stability": "dev", 13 | "require": { 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "There4\\Analytics\\": "src/", 18 | "Tests\\": "tests/" 19 | } 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "^6.5@dev" 23 | }, 24 | "scripts": { 25 | "test": "vendor/bin/phpunit" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests 14 | 15 | 16 | 17 | 18 | ./src 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/AnalyticsEvent.php: -------------------------------------------------------------------------------- 1 | 11 | * @created 07/15/2010 12 | */ 13 | class AnalyticsEvent 14 | { 15 | /** 16 | * @var string Google Analytics code 17 | */ 18 | private $_code; 19 | 20 | /** 21 | * @var string Domain name we are requesting from 22 | */ 23 | private $_domain; 24 | 25 | /** 26 | * @var string User Agent string for this request from CURL 27 | */ 28 | private $_useragent = 'PHPAnalyticsAgent/0.1 (http://there4development.com/)'; 29 | 30 | /** 31 | * @var string cookie name 32 | */ 33 | private $_cookie = "phpanalytics"; 34 | 35 | /** 36 | * @var bool verbose output 37 | */ 38 | private $_verbose = false; 39 | 40 | /** 41 | * Url for the google analytics gif 42 | * 43 | * http://code.google.com/intl/de-DE/apis/analytics/docs/tracking/ + 44 | * gaTrackingTroubleshooting.html#gifParameters 45 | * 46 | * @var string url for the gif string at google 47 | */ 48 | private $_urchin_url = 'http://www.google-analytics.com/__utm.gif'; 49 | 50 | /** 51 | * Setup Analytics 52 | * 53 | * @param string $code Google Analytics key (default: const GOOG_UA) 54 | * @param string $domain HTTP_HOST (default: $_SERVER['HTTP_HOST']) 55 | */ 56 | public function __construct($code = '', $domain = '') 57 | { 58 | $this->_code = !empty($code) ? $code : GOOG_UA; 59 | $this->_domain = 'No Domain Set'; 60 | 61 | if (!empty($domain)) { 62 | $this->_domain = $domain; 63 | } elseif (array_key_exists('HTTP_HOST', $_SERVER)) { 64 | $this->_domain = $_SERVER['HTTP_HOST']; 65 | } 66 | } 67 | 68 | /** 69 | * Track and event in Google Analytics 70 | * 71 | * http://code.google.com/apis/analytics/docs/tracking/ + 72 | * eventTrackerGuide.html 73 | * 74 | * @param string $object the name you supply for the group of objects 75 | * you want to track. 76 | * @param string $action A string that is uniquely paired with each category, 77 | * and commonly used to define the type of user 78 | * interaction for the web object. 79 | * @param string $label An optional string to provide additional dimensions 80 | * to the event data. 81 | * @param integer $value An integer that you can use to provide numerical 82 | * data about the user event. 83 | * 84 | * @return bool success 85 | */ 86 | public function trackEvent($object, $action, $label = '', $value = 1) 87 | { 88 | $var_utmac = $this->_code; 89 | $var_utmhn = $this->_domain; 90 | $var_utmn = rand(1000000000, PHP_INT_MAX); //random request number 91 | $var_cookie = rand(10000000, PHP_INT_MAX); //random cookie number 92 | $var_random = rand(1000000000, 2147483647); //number under 2147483647 93 | $var_today = time(); //today 94 | $var_referer = $this->getCurrentUrl(); //referer url 95 | $var_utmp = 'index.php'; 96 | $var_uservar = ''; 97 | 98 | $urchin_params = '' 99 | .'?utmwv=1' // Tracking code version 100 | .'&utmn='.$var_utmn // Prevent caching random number 101 | .'&utmsr=-' // Screen resolution 102 | .'&utmsc=-' // Screen color depth 103 | .'&utmul=-' // Browser language 104 | .'&utmje=0' // Is browser Java-enabled 105 | .'&utmfl=-' // Flash Version 106 | .'&utmdt=-' // Page title, url encoded 107 | .'&utmhn=' . $var_utmhn // Host Name 108 | .'&utmp=' . $var_utmp // page 109 | .'&utmr=' . $var_referer // Referral, complete url 110 | .'&utmac=' . $var_utmac // Account code 111 | .'&utmt=event' // Type of request 112 | // utme is an extensible parameter, used for the event data here 113 | ."&utme=" . rawurlencode("5($object*$action*$label)($value):") 114 | .'&utmcc=__utma%3D' . $var_cookie . '.' . $var_random . '.' . $var_today 115 | . '.' . $var_today . '.' . $var_today . '.2%3B%2B__utmb%3D' 116 | . $var_cookie . '%3B%2B__utmc%3D' . $var_cookie . '%3B%2B__utmz%3D' 117 | . $var_cookie . '.' . $var_today 118 | . '.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)' 119 | . '%7Cutmcmd%3D(none)%3B%2B__utmv%3D' 120 | . $var_cookie . '.' . $var_uservar . '%3B' 121 | ; // Cookie values are in this utmcc 122 | 123 | $url = $this->_urchin_url . $urchin_params; 124 | 125 | $ch = curl_init(); 126 | curl_setopt_array( 127 | $ch, 128 | array( 129 | CURLOPT_URL =>$url, 130 | CURLOPT_RETURNTRANSFER => true, 131 | CURLOPT_USERAGENT => $this->_useragent, 132 | CURLOPT_VERBOSE => $this->_verbose, 133 | CURLOPT_FOLLOWLOCATION => 1, 134 | CURLOPT_COOKIEFILE => $this->_cookie 135 | ) 136 | ); 137 | $output = curl_exec($ch); 138 | curl_close($ch); 139 | 140 | $is_gif = ('GIF89a' == substr($output, 0, 6)); 141 | 142 | return $is_gif; 143 | } 144 | 145 | /** 146 | * Get the current Url 147 | * 148 | * @return string current url 149 | */ 150 | public function getCurrentUrl() { 151 | $url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http'; 152 | $url .= '://' . $_SERVER['SERVER_NAME']; 153 | $url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT']; 154 | $url .= $_SERVER['REQUEST_URI']; 155 | return $url; 156 | } 157 | } 158 | 159 | /* End of file AnalyticsEvent.php */ 160 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf( 15 | AnalyticsEvent::class, 16 | new AnalyticsEvent('UAxxxxxxx', 'example.com') 17 | ); 18 | } 19 | } 20 | 21 | /* End of file SanityTest.php */ 22 | --------------------------------------------------------------------------------