Commit 5040a928 authored by Avris's avatar Avris

init

parents
tests/_output/*
vendor/*
.idea/*
**/.DS_Store
## 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:
- Avris\Micrus\GoogleAnalytics\GoogleAnalyticsModule
Then run:
composer require avris/micrus-ga
### 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:
$this->get('googleAnalytics')->collect($this->getRequest());
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.
### Copyright ###
* **Author:** Andrzej Prusinowski [(Avris.it)](https://avris.it)
* **Licence:** [MIT](https://mit.avris.it)
{
"name": "avris/micrus-ga",
"type": "library",
"description": "Google Analytics service the Micrus Framework",
"license": "MIT",
"homepage": "https://micrus.avris.it",
"authors": [{
"name": "Avris",
"email": "andre@avris.it",
"homepage": "https://avris.it"
}],
"require": {
"avris/micrus": "^3.0"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\GoogleAnalytics\\": "src" }
}
}
<?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;
class GoogleAnalytics extends \Twig_Extension implements ParametersProvider
{
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');
</script>
HTML;
const COLLECT_URL = 'https://www.google-analytics.com/collect';
/** @var string */
protected $trackingId;
/** @var string[] */
protected $queue = [];
public function __construct(Bag $config)
{
$this->trackingId = $config->get('trackingId');
}
/** @return array */
public function getDefaultParameters()
{
return [
'googleAnalytics' => [
'trackingId' => 'GOOGLE_ANALYTICS_TRACKING_ID',
]
];
}
public function getFunctions()
{
return [
'googleAnaytics' => new \Twig_SimpleFunction('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)
{
if (!$this->trackingId) {
return false;
}
$data = [
'v' => 1,
'tid' => $this->trackingId,
'cid' => sha1(serialize($request)),
't' => 'pageview',
'dl' => $request->getFullUrl(),
'ul' => ($languages = $request->getHeaders()->getLanguage()) ? $languages->getBest() : null,
'de' => ($charset = $request->getHeaders()->getCharset()) ? $charset->getBest() : null,
];
$options = [
CURLOPT_URL => static::COLLECT_URL,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-type: application/x-www-form-urlencoded'],
CURLOPT_USERAGENT => (string) $request->getHeaders()->getUserAgent(),
CURLOPT_POSTFIELDS => utf8_encode(http_build_query($data)),
CURLOPT_RETURNTRANSFER => true,
];
if ($deferred) {
$this->queue[] = $options;
return true;
}
$this->sendReport($options);
return true;
}
protected 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;
class GoogleAnalyticsModule implements Module
{
public function extendConfig($env, $rootDir)
{
return [
'services' => [
'googleAnalytics' => [
'class' => GoogleAnalytics::class,
'params' => ['@config.parameters.?googleAnalytics'],
'events' => ['terminate'],
'tags' => ['defaultParameters', 'twigExtension'],
],
],
];
}
}
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