Skip to content
Snippets Groups Projects
Commit 189f7b68 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

hotline #158319 : fix google analytics id detection

parent 8f5c41fc
Branches
Tags
2 merge requests!4505hotline#147634 : pre-registration : mail sent now can use placeholders...,!4484hotline #158319 : fix google analytics id detection
Pipeline #17755 passed with stage
in 19 minutes and 46 seconds
- correctif #158319 : RGPD : Détection des identifiants Google Analytics au format G-XXXXX
\ No newline at end of file
......@@ -59,14 +59,17 @@ class Class_AdminVar_JsStat {
}
protected function _getGoogleAnalyticsId() {
protected function _getGoogleAnalyticsId() : string {
if ($id = Class_AdminVar::get('GOOGLE_ANALYTICS_ID'))
return $id;
return $this->_extractFromJsStat("/(UA|MO)-[0-9]+-[0-9]/", 0);
return ($id = $this->_extractFromJsStat('/(UA|MO)-[0-9]+-[0-9]/', 0))
? $id
: $this->_extractFromJsStat('/G-[a-zA-Z0-9]+/', 0);
}
protected function _extractFromJsStat($pattern, $match_index) {
protected function _extractFromJsStat($pattern, $match_index) : string {
preg_match($pattern,
Class_AdminVar::get('JS_STAT'),
$matches);
......
......@@ -20,44 +20,34 @@
*/
class CookiesManager_AnalyticsGoogleTest extends AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
abstract class CookiesManager_AnalyticsGoogleTestCase extends AbstractControllerTestCase {
public function setUp() {
parent::setUp();
$this->fixture('Class_AdminVar', ['id' =>'JS_STAT',
'valeur' => "<script> var _gaq = _gaq ||
[]; _gaq.push(['_setAccount',
'UA-41754005-1']);
_gaq.push(['_trackPageview']);
(function() { var ga =
document.createElement('script');
ga.type = 'text/javascript'; ga.async =
true; ga.src = ('https:' ==
document.location.protocol ?
'https://ssl' : 'http://www') +
'.google-analytics.com/ga.js'; var
s =
document.getElementsByTagName('script')[
0]; s.parentNode.insertBefore(ga, s);
})(); </script>"]);
$this->fixture(Class_AdminVar::class,
['id' => 'JS_STAT',
'valeur' => $this->_getGoogleScript()]);
}
abstract protected function _getGoogleScript() : string;
abstract protected function _getGoogleId() : string;
/** @test */
public function pageShouldIncludeGoogleAnalyticOnTarteAuCitron() {
Class_AdminVar_Cookies::setManager(new Class_Cookies_TarteAuCitron());
$this->dispatch('/index', true);
$this->assertXPathContentContains('//script', 'tarteaucitron.user.analyticsUa = "UA-41754005-1";');
public function withoutCookiesManagerPageShouldIncludeRawGoogleAnalytics() {
Class_AdminVar_Cookies::setManager(null);
$this->dispatch('/index');
$this->assertContains($this->_getGoogleScript(), $this->_response->getBody());
}
/** @test */
public function pageShouldIncludeGoogleAnalyticOn() {
Class_AdminVar_Cookies::setManager(null);
$this->dispatch('/index', true);
$this->assertXPathContentContains('//script', '_gaq.push([\'_setAccount');
public function withTarteaucitronManagerPageShouldIncludeGoogleAnalyticViaTarteaucitron() {
Class_AdminVar_Cookies::setManager(new Class_Cookies_TarteAuCitron);
$this->dispatch('/index');
$this->assertXPathContentContains('//script',
'tarteaucitron.user.analyticsUa = "'. $this->_getGoogleId() . '";');
}
......@@ -76,7 +66,7 @@ class CookiesManager_AnalyticsGoogleTest extends AbstractControllerTestCase {
public function pageShouldWithTarteaucitronIncludeGoogleAnalyticSpecifiedIdInsteadOfJs() {
Class_AdminVar::set('GOOGLE_ANALYTICS_ID', 'UA-test');
Class_AdminVar::set('GOOGLE_ANALYTICS_MOREJS', '_var myvar="test"; ga.push(myvar);');
Class_AdminVar_Cookies::setManager(new Class_Cookies_TarteAuCitron());
Class_AdminVar_Cookies::setManager(new Class_Cookies_TarteAuCitron);
$this->dispatch('/index', true);
$this->assertXPathContentContains('//script', 'tarteaucitron.user.analyticsUa = "UA-test";');
$this->assertXPathContentContains('//script', 'tarteaucitron.user.analyticsMore = function () {_var myvar="test"; ga.push(myvar);};');
......@@ -86,10 +76,59 @@ class CookiesManager_AnalyticsGoogleTest extends AbstractControllerTestCase {
class CookiesManager_AnalyticsMatomoTest extends AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
class CookiesManager_AnalyticsGoogleUATest extends CookiesManager_AnalyticsGoogleTestCase {
protected function _getGoogleScript() : string {
return "
<script> var _gaq = _gaq ||
[]; _gaq.push(['_setAccount',
'UA-41754005-1']);
_gaq.push(['_trackPageview']);
(function() { var ga =
document.createElement('script');
ga.type = 'text/javascript'; ga.async =
true; ga.src = ('https:' ==
document.location.protocol ?
'https://ssl' : 'http://www') +
'.google-analytics.com/ga.js'; var
s =
document.getElementsByTagName('script')[
0]; s.parentNode.insertBefore(ga, s);
})(); </script>
";
}
protected function _getGoogleId() : string {
return 'UA-41754005-1';
}
}
class CookiesManager_AnalyticsGoogleGTest extends CookiesManager_AnalyticsGoogleTestCase {
protected function _getGoogleScript() : string {
return "
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src=\"https://www.googletagmanager.com/gtag/js?id=G-PJBYBXEL12\"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-PJBYBXEL12');
</script>";
}
protected function _getGoogleId() : string {
return 'G-PJBYBXEL12';
}
}
class CookiesManager_AnalyticsMatomoTest extends AbstractControllerTestCase {
public function setUp() {
parent::setUp();
......
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