|
| 1 | +# Analytics tracking package for Laravel 4.x |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +Add to your composer.json following lines |
| 6 | + |
| 7 | + "repositories": [ |
| 8 | + { |
| 9 | + "type": "vcs", |
| 10 | + "url": "https://github.com/ipunkt/laravel-analytics" |
| 11 | + } |
| 12 | + ], |
| 13 | + "require": { |
| 14 | + "ipunkt/laravel-analytics": "dev-master" |
| 15 | + } |
| 16 | + |
| 17 | +Run `php artisan config:publish ipunkt/laravel-analytics` |
| 18 | + |
| 19 | +Then edit `analytics.php` in `app/config/packages/ipunkt/laravel-analytics` to your needs. |
| 20 | + |
| 21 | +Add `'Ipunkt\LaravelAnalytics\AnalyticsServiceProvider',` to `providers` in `app/config/app.php`. |
| 22 | + |
| 23 | +Add `'Analytics' => 'Ipunkt\LaravelAnalytics\AnalyticsFacade',` to `aliases` in `app/config/app.php`. |
| 24 | + |
| 25 | + |
| 26 | +## Configuration |
| 27 | + |
| 28 | + provider - Provider to use, possible Providers are: GoogleAnalytics, NoAnalytics |
| 29 | + |
| 30 | +### Google Analytics |
| 31 | + |
| 32 | + tracking_id - Tracking ID |
| 33 | + |
| 34 | + tracking_domain - Tracking domain, unset or set to "auto" for automatic fallback |
| 35 | + |
| 36 | + anonymize_ip - (true|false) anonymize users ip |
| 37 | + |
| 38 | + auto_track - (true|false) auto tracking current pageview |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +In controller action (or anywhere else) use following statement to track an event or page view: |
| 43 | + |
| 44 | + // tracking the current page view |
| 45 | + Analytics::trackPage(); // only necessary if `auto_track` is false or Analytics::disableAutoTracking() was called before |
| 46 | + |
| 47 | + // tracking an event |
| 48 | + Analytics::trackEvent('category', 'action'); |
| 49 | + |
| 50 | + // tracking a custom line |
| 51 | + Analytics::trackCustom("ga('send', ......"); // this line will be added within the tracking script |
| 52 | + |
| 53 | +In your view or layout template (e.g. a blade template) use the following statement: |
| 54 | + |
| 55 | + {{ Analytics::render() }} |
| 56 | + |
| 57 | +For Google Analytics you should place the statement right behind the `body` tag |
| 58 | + |
| 59 | + <body>{{ Analytics::render() }} |
| 60 | + |
| 61 | +## How to use |
| 62 | + |
| 63 | +The `GoogleAnalytics` Provider automatically controls the local environment behaviour for testing purposes. |
| 64 | +See https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#localhost for details. |
| 65 | + |
| 66 | +There is a builtin provider called `NoAnalytics`. This is for testing environments and tracks nothing. So you do |
| 67 | +not have to rewrite your code, simple select this `provider` in `analytics` configuration for your special environment |
| 68 | +configurations. |
| 69 | + |
| 70 | + |
| 71 | +## API Documentation |
| 72 | + |
| 73 | +For the correct usage methods look at the `Ipunkt\LaravelAnalytics\Contracts\AnalyticsProviderInterface.php` |
| 74 | + |
| 75 | +### Analytics::render() |
| 76 | + |
| 77 | +For rendering the correct javascript code. |
| 78 | + |
| 79 | + /** |
| 80 | + * returns the javascript code for embedding the analytics stuff |
| 81 | + * |
| 82 | + * @return string |
| 83 | + */ |
| 84 | + public function render(); |
| 85 | + |
| 86 | + |
| 87 | +### Analytics::trackPage() |
| 88 | + |
| 89 | +For tracking a page view. |
| 90 | + |
| 91 | + /** |
| 92 | + * track an page view |
| 93 | + * |
| 94 | + * @param null|string $page |
| 95 | + * @param null|string $title |
| 96 | + * @param null|string $hittype |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function trackPage($page, $title, $hittype); |
| 100 | + |
| 101 | + |
| 102 | +### Analytics::trackEvent() |
| 103 | + |
| 104 | +For tracking an event |
| 105 | + |
| 106 | + /** |
| 107 | + * track an event |
| 108 | + * |
| 109 | + * @param string $category |
| 110 | + * @param string $action |
| 111 | + * @param null|string $label |
| 112 | + * @param null|int $value |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + public function trackEvent($category, $action, $label, $value); |
| 116 | + |
| 117 | + |
| 118 | +### Analytics::trackCustom() |
| 119 | + |
| 120 | +For tracking a custom script line within the embedded analytics code. |
| 121 | + |
| 122 | + /** |
| 123 | + * track any custom code |
| 124 | + * |
| 125 | + * @param string $customCode |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function trackCustom($customCode); |
| 129 | + |
| 130 | + |
| 131 | +### Analytics::enableAutoTracking() |
| 132 | + |
| 133 | +Enabling the auto tracking, overriding the configuration setting `auto_track`. |
| 134 | + |
| 135 | + /** |
| 136 | + * enable auto tracking |
| 137 | + * |
| 138 | + * @return void |
| 139 | + */ |
| 140 | + public function enableAutoTracking(); |
| 141 | + |
| 142 | + |
| 143 | +### Analytics::disableAutoTracking() |
| 144 | + |
| 145 | +Disabling the auto tracking, overriding the configuration setting `auto_track`. |
| 146 | + |
| 147 | + /** |
| 148 | + * disable auto tracking |
| 149 | + * |
| 150 | + * @return void |
| 151 | + */ |
| 152 | + public function disableAutoTracking(); |
| 153 | + |
0 commit comments