Browse Source

Modification of the piwik script:

 * Put the script juste before the body tag (and not the head tag)
 * Add tracking by user login (if any)
 * If the page isn't an HTML page, we don't add the piwik code (ie: in js like
   ckeditor.js that contains a </head> tag).
 * Update the piwik script with the last scripts (piwik 2.9.0)
tags/0.1.0
Ulrich Van Den Hekke 5 years ago
parent
commit
97eadb8251

+ 15
- 0
Common/PiwikStorage.php View File

@@ -18,9 +18,24 @@ class PiwikStorage {
protected $goals = array();
protected $piwikUser = null;
protected $pageName = null;
protected $pageUrl = null;
/**
* Set the page User.
*/
public function setPiwikUser($piwikUser) {
$this->piwikUser = $piwikUser;
}
/**
* Return the page User.
*/
public function getPiwikUser() {
return $this->piwikUser;
}
/**
* Set the page name.
*/

+ 14
- 1
EventListener/PiwikListener.php View File

@@ -128,6 +128,15 @@ class PiwikListener implements EventSubscriberInterface {
$this->save();
}
public function getPiwikUser() {
return $this->storage->getPiwikUser();
}
public function setPiwikUser($value) {
$this->storage->setPiwikUser($value);
$this->save();
}
public function setCustomVariable ($index, $name, $value, $scope = "visit") {
$this->storage->setCustomVariable($index, $name, $value, $scope);
$this->save();
@@ -190,6 +199,10 @@ class PiwikListener implements EventSubscriberInterface {
if ($request->isXmlHttpRequest() || $response->isRedirection() || $response->getContent() == null) {
return;
}
// If we the content type isn't an html application we reject it.
if (strpos($response->headers->get('Content-Type'), "text/html") === false) {
return;
}
$this->injectPiwik($response);
}
@@ -205,7 +218,7 @@ class PiwikListener implements EventSubscriberInterface {
}
$content = $response->getContent();
$posEndHead = $posrFunction($content, '</head>');
$posEndHead = $posrFunction($content, '</body>');
if (false !== $posEndHead) {
$piwik_js = "\n".str_replace("\n", '', $this->templating->render(

+ 19
- 16
Resources/views/Default/async_piwik_js.html.twig View File

@@ -10,13 +10,10 @@
<!-- Piwik -->
<script type="text/javascript">/*<![CDATA[*/
var _paq = _paq || [];
{% if not hidePiwik %}
(function(){ var u=(("https:" == document.location.protocol) ? "https://{{ url }}/" : "http://{{ url }}/");
_paq.push(['setTrackerUrl', u+'piwik.php']);
{% else %}
(function(){ _paq.push(['setTrackerUrl', '{{ url('shadoware_piwik_default_track') }}']);
{# Piwik user #}
{% if storage.piwikUser %}
_paq.push(['setUserId', '{{ storage.piwikUser }}']);
{% endif %}
_paq.push(['setSiteId', {{ id_site }}]);

{# Heartbeat #}
{% if heartbeatEnabled %}
@@ -26,7 +23,7 @@ _paq.push(['setHeartBeatTimer', {{ heartbeatMinimumVisitLength }}, {{ heartbeatD
{# Custom variables #}
{% for scope, array in storage.customVariables %}
{% for index, customVariable in array %}
_paq.push(['setCustomVariable', {{ index }}, '{{ customVariable.name | e('js') }}', '{{ customVariable.value | e('js') }}', '{{ scope }}']);
_paq.push(['setCustomVariable', {{ index }}, '{{ customVariable.name | e('js') }}', {{ customVariable.value | raw }}, '{{ scope }}']);
{% endfor %}
{% endfor %}

@@ -80,9 +77,7 @@ _paq.push(['trackEcommerceOrder',

{# E-Commerce - Basket #}
{% if storage.ecommerceCartUpdate and storage.ecommerceItems | length > 0 %}
_paq.push(['trackEcommerceCartUpdate',
{{ storage.ecommerceCartUpdate.amount }}
]);
_paq.push(['trackEcommerceCartUpdate', {{ storage.ecommerceCartUpdate.amount }}]);
{% endif %}

{# Goals #}
@@ -92,18 +87,26 @@ _paq.push(['trackGoal', {{ goal.id }}{% if goal.amount %}, {{ goal.amount }}{% e

{# Enable link tracking #}
_paq.push(['trackPageView']);

{# Really track the page #}
_paq.push(['enableLinkTracking']);

{# Definition of the Piwik script #}
{% if not hidePiwik %}
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+'piwik.js';
s.parentNode.insertBefore(g,s); })();
(function(){
var u="//{{ url }}/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', {{ id_site }}]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'js/piwik.js'; s.parentNode.insertBefore(g,s);
})();
{% else %}
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src='{{ url('shadoware_piwik_default_tracker', {'_format': 'js'}) }}';
s.parentNode.insertBefore(g,s); })();
(function(){
_paq.push(['setTrackerUrl', '{{ url('shadoware_piwik_default_track') }}']);
_paq.push(['setSiteId', {{ id_site }}]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src='{{ url('shadoware_piwik_default_tracker', {'_format': 'js'}) }}'; s.parentNode.insertBefore(g,s);
})();
{% endif %}

/*]]>*/</script>
<!-- End Piwik Code -->
{% endspaceless %}

+ 5
- 0
Twig/PiwikExtension.php View File

@@ -32,6 +32,7 @@ class PiwikExtension extends \Twig_Extension {
'setEcommerceView' => new \Twig_Function_Method($this, 'setEcommerceView'),
'setPiwikPageName' => new \Twig_Function_Method($this, 'setPageName'),
'setPiwikPageUrl' => new \Twig_Function_Method($this, 'setPageUrl'),
'setPiwikUser' => new \Twig_Function_Method($this, 'setPiwikUser'),
);
}
@@ -75,6 +76,10 @@ class PiwikExtension extends \Twig_Extension {
$this->container->get('shadoware_piwik.service')->setPageUrl($pageUrl);
}
public function setPiwikUser($pageUser) {
$this->container->get('shadoware_piwik.service')->setPiwikUser($pageUser);
}
public function getName() {
return 'piwik_extension';
}

Loading…
Cancel
Save