├── .github └── dependabot.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── _config.yml ├── composer.json └── src ├── Facades └── GAMP.php ├── LaravelGAMPServiceProvider.php └── config └── gamp.php /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: composer 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "23:30" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | composer.lock 3 | vendor 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2015-19 Syed Irfaq R. 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel-GAMP Package 2 | ===================== 3 | [![Join PHP Chat][ico-phpchat]][link-phpchat] 4 | [![Chat on Telegram][ico-telegram]][link-telegram] 5 | [![Laravel & Lumen Package][ico-package]][link-repo] 6 | [![Latest Version on Packagist][ico-version]][link-packagist] 7 | [![Software License][ico-license]][link-license] 8 | [![Total Downloads][ico-downloads]][link-downloads] 9 | 10 | > Laravel GAMP: Google Analytics Measurement Protocol Package for Laravel & Lumen. 11 | > 12 | > Send data to Google Analytics from Laravel/Lumen. Supports all GA Measurement Protocol API methods. 13 | 14 | [![Laravel GAMP][cover-img]][link-author] 15 | 16 | 17 | ## Quick start 18 | 19 | ### Install 20 | 21 | #### Step 1: Install Through Composer 22 | 23 | ```bash 24 | $ composer require irazasyed/laravel-gamp 25 | ``` 26 | 27 | #### (Lumen) Add the Service Provider 28 | 29 | Open `bootstrap/app.php` and register the service provider by adding: 30 | 31 | ```php 32 | $app->register(Irazasyed\LaravelGAMP\LaravelGAMPServiceProvider::class); 33 | ``` 34 | 35 | #### Step 2: Publish Config 36 | 37 | Open your terminal window and fire the following command to publish config file to your config directory: 38 | 39 | ``` bash 40 | $ php artisan vendor:publish --provider="Irazasyed\LaravelGAMP\LaravelGAMPServiceProvider" 41 | ``` 42 | 43 | ## Usage 44 | 45 | Open config file `config/gamp.php` and set the `tracking_id` with your Google Analytics tracking / web property ID. 46 | Refer the config file for other default configuration settings. 47 | 48 | This Package adds Laravel Support to [GA Measurement Protocol][link-lib] PHP Library by THE ICONIC. 49 | It's simply a wrapper around the library with default config for easier usage with Laravel. 50 | So all the methods listed [here][link-docs] are available and will work seamlessly. 51 | 52 | ### Example Usage 53 | 54 | Send a Page view hit: 55 | 56 | ``` php 57 | use Irazasyed\LaravelGAMP\Facades\GAMP; 58 | 59 | $gamp = GAMP::setClientId( '123456' ); 60 | $gamp->setDocumentPath( '/page' ); 61 | $gamp->sendPageview(); 62 | ``` 63 | 64 | Send an Event: 65 | 66 | ``` php 67 | use Irazasyed\LaravelGAMP\Facades\GAMP; 68 | 69 | $gamp = GAMP::setClientId( '123456' ); 70 | $gamp->setEventCategory('Blog Post') 71 | ->setEventAction('Create') 72 | ->setEventLabel('Using GAMP In Laravel') 73 | ->sendEvent(); 74 | ``` 75 | 76 | ### Config Overview 77 | 78 | Open the config file for detailed comments for each option. 79 | 80 | Set your Google Analytics Tracking / Web Property ID in `tracking_id` key **[REQUIRED]** 81 | 82 | ``` php 83 | 'tracking_id' => 'UA-XXXX-Y', 84 | ``` 85 | 86 | All other configuration options are optional, use as per your requirements. 87 | 88 | To send data over SSL, set `is_ssl` to true. 89 | 90 | ``` php 91 | 'is_ssl' => true, 92 | ``` 93 | 94 | To disable library in Staging/Dev env (Prevents the library from sending any hits to GA), set `is_disabled` to true. 95 | 96 | ``` php 97 | 'is_disabled' => true, 98 | ``` 99 | 100 | To Anonymize IP, set `anonymize_ip` to true. 101 | 102 | ``` php 103 | 'anonymize_ip' => true, 104 | ``` 105 | 106 | To Make Async Requests, set `async_requests` to true. 107 | 108 | ``` php 109 | 'async_requests' => true, 110 | ``` 111 | 112 | ... 113 | 114 | Refer the library's [documentation][link-docs] for other remaining methods and examples, they all work. 115 | 116 | > **Note:** You don't have to use the protocol version, tracking id, anonymize ip and async request (non-blocking) methods from the original library as they're automatically set in Service Provider when the package is initialized based on the config file. As long as you update the config file with correct settings, it should work just fine. 117 | 118 | ## Additional information 119 | 120 | Any issues, please [report here][link-issues] 121 | 122 | ## Credits 123 | 124 | - [Syed Irfaq R.][link-author] - For Laravel and Lumen Bridge. 125 | - [THE ICONIC][link-lib] - For GA Measurement Protocol PHP Library. 126 | - [All Contributors][link-contributors] 127 | 128 | ## License 129 | 130 | The MIT License (MIT). Please see [License File][link-license] for more information. 131 | 132 | [cover-img]: https://cloud.githubusercontent.com/assets/1915268/8476296/b49f74ac-20dd-11e5-8698-aa23b2f7e6fd.png 133 | [ico-phpchat]: https://img.shields.io/badge/Slack-PHP%20Chat-5c6aaa.svg?style=flat-square&logo=slack&labelColor=4A154B 134 | [ico-telegram]: https://img.shields.io/badge/@PHPChatCo-2CA5E0.svg?style=flat-square&logo=telegram&label=Telegram 135 | [ico-version]: https://img.shields.io/packagist/v/irazasyed/laravel-gamp.svg?style=flat-square 136 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 137 | [ico-downloads]: https://img.shields.io/packagist/dt/irazasyed/laravel-gamp.svg?style=flat-square 138 | [ico-package]: https://img.shields.io/badge/Laravel-5~10-FF2D20.svg?style=flat-square&logo=laravel&labelColor=black&logoColor=white 139 | 140 | [link-phpchat]: https://phpchat.co/?ref=laravel-gamp 141 | [link-telegram]: https://t.me/PHPChatCo 142 | [link-author]: https://github.com/irazasyed 143 | [link-repo]: https://github.com/irazasyed/laravel-gamp 144 | [link-license]: LICENSE.md 145 | [link-issues]: ../../issues 146 | [link-contributors]: ../../contributors 147 | [link-lib]: https://github.com/theiconic/php-ga-measurement-protocol 148 | [link-docs]: https://github.com/theiconic/php-ga-measurement-protocol#usage 149 | [link-packagist]: https://packagist.org/packages/irazasyed/laravel-gamp 150 | [link-downloads]: https://packagist.org/packages/irazasyed/laravel-gamp/stats 151 | [link-sensiolabs]: https://insight.sensiolabs.com/projects/880d79a9-7bab-4872-ab98-76b2e53429e9 152 | 153 | 154 | [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Firazasyed%2Flaravel-gamp.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Firazasyed%2Flaravel-gamp?ref=badge_large) 155 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "irazasyed/laravel-gamp", 3 | "description": "Send analytics data to Google Analytics from Laravel. A package for GA Measurement Protocol API", 4 | "keywords": [ 5 | "laravel google analytics", 6 | "lumen google analytics", 7 | "google analytics", 8 | "analytics", 9 | "tracking", 10 | "ga measurement protocol", 11 | "measurement protocol" 12 | ], 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Syed Irfaq R.", 17 | "email": "syed+gh@lukonet.com", 18 | "homepage": "https://github.com/irazasyed" 19 | } 20 | ], 21 | "require": { 22 | "php": "^7.2 || ^8.0", 23 | "illuminate/support": "^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", 24 | "theiconic/php-ga-measurement-protocol": "^2.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Irazasyed\\LaravelGAMP\\": "src/" 29 | } 30 | }, 31 | "extra": { 32 | "branch-alias": { 33 | "dev-master": "1.0-dev" 34 | }, 35 | "laravel": { 36 | "providers": [ 37 | "Irazasyed\\LaravelGAMP\\LaravelGAMPServiceProvider" 38 | ], 39 | "aliases": { 40 | "GAMP": "Irazasyed\\LaravelGAMP\\Facades\\GAMP" 41 | } 42 | } 43 | }, 44 | "suggest": { 45 | "irazasyed/larasupport": "Adds Package Support in Lumen, Lets you install any Laravel Package." 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Facades/GAMP.php: -------------------------------------------------------------------------------- 1 | app instanceof LaravelApplication && $this->app->runningInConsole()) { 20 | $this->publishes([$source => config_path('gamp.php')]); 21 | } elseif ($this->app instanceof LumenApplication) { 22 | $this->app->configure('gamp'); 23 | } 24 | 25 | $this->mergeConfigFrom($source, 'gamp'); 26 | } 27 | 28 | /** 29 | * Register the service provider. 30 | */ 31 | public function register() 32 | { 33 | $this->app->bind(Analytics::class, function ($app) { 34 | $config = $app['config']; 35 | 36 | $analytics = new Analytics($config->get('gamp.is_ssl', false), $config->get('gamp.is_disabled', false)); 37 | 38 | $analytics->setProtocolVersion($config->get('gamp.protocol_version', 1)) 39 | ->setTrackingId($config->get('gamp.tracking_id')); 40 | 41 | if ($config->get('gamp.anonymize_ip', false)) { 42 | $analytics->setAnonymizeIp('1'); 43 | } 44 | 45 | if ($config->get('gamp.async_requests', false)) { 46 | $analytics->setAsyncRequest(true); 47 | } 48 | 49 | return $analytics; 50 | }); 51 | 52 | $this->app->alias(Analytics::class, 'gamp'); 53 | } 54 | 55 | /** 56 | * Get the services provided by the provider. 57 | * 58 | * @return array 59 | */ 60 | public function provides() 61 | { 62 | return [Analytics::class, 'gamp']; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/config/gamp.php: -------------------------------------------------------------------------------- 1 | env('GA_ID'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Measurement Protocol Version [REQUIRED] 21 | |-------------------------------------------------------------------------- 22 | | 23 | | The Protocol version. The current value is '1'. 24 | | This will only change when there are changes made that are not backwards compatible. 25 | | 26 | | Refer: 27 | | https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#v 28 | | 29 | | Default: 1 30 | | 31 | */ 32 | 'protocol_version' => 1, 33 | 34 | /* 35 | |-------------------------------------------------------------------------- 36 | | URL Endpoint - SSL Support: Send Data over SSL [Optional] 37 | |-------------------------------------------------------------------------- 38 | | 39 | | This option controls the URL endpoint of the Measurement Protocol. 40 | | To send data over SSL, set true. 41 | | 42 | | Refer: 43 | | https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tid 44 | | 45 | | Default: false 46 | | Valid Values: (Boolean) "true" OR "false" 47 | | 48 | */ 49 | 'is_ssl' => false, 50 | 51 | /* 52 | |-------------------------------------------------------------------------- 53 | | Disable Hits [Optional] 54 | |-------------------------------------------------------------------------- 55 | | 56 | | This option controls enabling or disabling the library. 57 | | Useful in Staging/Dev environments when you don't want to actually send hits to GA. 58 | | When disabled, it returns a AnalyticsResponseInterface object that returns empty values. 59 | | 60 | | To disable library hits, set true. 61 | | 62 | | Default: false 63 | | Valid Values: (Boolean) "true" OR "false" 64 | | 65 | */ 66 | 'is_disabled' => false, 67 | 68 | /* 69 | |-------------------------------------------------------------------------- 70 | | Anonymize IP [Optional] 71 | |-------------------------------------------------------------------------- 72 | | 73 | | When set to True, the IP address of the sender will be anonymized. 74 | | 75 | | Refer: 76 | | https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aip 77 | | 78 | | Default: false 79 | | Valid Values: (Boolean) "true" OR "false" 80 | | 81 | */ 82 | 'anonymize_ip' => false, 83 | 84 | /* 85 | |-------------------------------------------------------------------------- 86 | | Asynchronous Requests [Optional] 87 | |-------------------------------------------------------------------------- 88 | | 89 | | When set to True, All the requests would be made non-blocking (Async). 90 | | 91 | | Default: false 92 | | Valid Values: (Boolean) "true" OR "false" 93 | | 94 | */ 95 | 'async_requests' => false, 96 | ]; 97 | --------------------------------------------------------------------------------