Commit 4f3368aa authored by Avris's avatar Avris

Merge branch 'v4.0' into 'master'

v4.0

See merge request !1
parents 5040a928 eb6da494
tests/_output/*
vendor/*
.idea/*
/.idea/*
**/.DS_Store
/vendor/*
/tests/_output/*
## Micrus Google Analytics Service ##
# Micrus Google Analytics Service ##
This is a module for [Micrus framework](https://micrus.avris.it)
that adds a really basic helper for Google Analytics.
To install this module, open the file `app/Config/modules.yml` and add:
## Installation
- Avris\Micrus\GoogleAnalytics\GoogleAnalyticsModule
Then run:
Run:
composer require avris/micrus-ga
### Usage ###
Then register the module in your `App\App:registerModules`:
yield new \Avris\Micrus\GoogleAnalytics\DoctrineModule;
You will be asked for a GA Tracking-ID and it will be saved in the `.env` file.
If you don't provide any (for instance in a testing environment),
this service will not fail, it will just do nothing.
## Usage ###
To include a tracking code in your template, just write before the closing `</body>`:
{{ googleAnaytics() }}
To report a visit from the server side, you can include this code:
To report a visit from the server side, you can run this code:
$this->get('googleAnalytics')->collect($this->getRequest());
public function generateImageAction(RequestInterface $request, GoogleAnalytics $ga)
{
// ...
$ga->collect($request);
// ...
}
Note that if you don't provide any Tracking-ID in your `parameters.yml`,
no action will be taken in any of those two cases.
For performance, that data will be sent during request termination
(so in the background, after the response has already been sent).
You can force the service to send the data right away with `$ga->collect($request, false)`.
### Copyright ###
## Copyright
* **Author:** Andrzej Prusinowski [(Avris.it)](https://avris.it)
* **Author:** Andre Prusinowski [(Avris.it)](https://avris.it)
* **Licence:** [MIT](https://mit.avris.it)
......@@ -10,7 +10,8 @@
"homepage": "https://avris.it"
}],
"require": {
"avris/micrus": "^3.0"
"avris/micrus": "^4.0",
"avris/micrus-twig": "^4.0"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\GoogleAnalytics\\": "src" }
......
Avris\Micrus\GoogleAnalytics\:
dir: '%MODULE_DIR%/src/'
Avris\Micrus\GoogleAnalytics\GoogleAnalytics:
public: true
<?php
namespace Avris\Micrus\GoogleAnalytics;
use Avris\Bag\Bag;
use Avris\Micrus\Bootstrap\TerminateEvent;
use Avris\Micrus\Controller\Http\RequestInterface;
use Avris\Micrus\Tool\Config\ParametersProvider;
use Avris\Micrus\Bootstrap\Terminator;
use Avris\Http\Request\RequestInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class GoogleAnalytics extends \Twig_Extension implements ParametersProvider
final class GoogleAnalytics extends AbstractExtension
{
const CODE = <<<HTML
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '%s', 'auto');
ga('send', 'pageview');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '%s');
</script>
HTML;
const COLLECT_URL = 'https://www.google-analytics.com/collect';
/** @var string */
protected $trackingId;
private $trackingId;
/** @var string[] */
protected $queue = [];
/** @var array[] */
private $queue = [];
public function __construct(Bag $config)
public function __construct(string $envGaTrackingId, Terminator $terminator)
{
$this->trackingId = $config->get('trackingId');
}
/** @return array */
public function getDefaultParameters()
{
return [
'googleAnalytics' => [
'trackingId' => 'GOOGLE_ANALYTICS_TRACKING_ID',
]
];
$this->trackingId = $envGaTrackingId;
$terminator->attach(function () {
foreach ($this->queue as $options) {
$this->sendReport($options);
}
});
}
public function getFunctions()
{
return [
'googleAnaytics' => new \Twig_SimpleFunction('googleAnaytics', function () {
new TwigFunction('googleAnaytics', function () {
return $this->trackingId ? sprintf(static::CODE, $this->trackingId) : '';
}, ['is_safe' => ['html']]),
];
}
/**
* @param RequestInterface $request
* @param bool $deferred
* @return bool
*/
public function collect(RequestInterface $request, $deferred = true)
public function collect(RequestInterface $request, bool $deferred = true): bool
{
if (!$this->trackingId) {
return false;
......@@ -94,18 +81,11 @@ HTML;
return true;
}
protected function sendReport($options)
private function sendReport($options)
{
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
public function onTerminate()
{
foreach ($this->queue as $options) {
$this->sendReport($options);
}
}
}
<?php
namespace Avris\Micrus\GoogleAnalytics;
use Avris\Micrus\Bootstrap\Module;
use Avris\Micrus\Bootstrap\ModuleInterface;
use Avris\Micrus\Bootstrap\ModuleTrait;
use Avris\Micrus\Tool\Config\ParametersProvider;
class GoogleAnalyticsModule implements Module
final class GoogleAnalyticsModule implements ModuleInterface, ParametersProvider
{
public function extendConfig($env, $rootDir)
use ModuleTrait;
public function getParametersDefaults(): array
{
return [
'services' => [
'googleAnalytics' => [
'class' => GoogleAnalytics::class,
'params' => ['@config.parameters.?googleAnalytics'],
'events' => ['terminate'],
'tags' => ['defaultParameters', 'twigExtension'],
],
],
'GA_TRACKING_ID' => '',
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment