-
Notifications
You must be signed in to change notification settings - Fork 6
/
googleAnalytics.hooks.php
87 lines (72 loc) · 2.44 KB
/
googleAnalytics.hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
class GoogleAnalyticsHooks {
/**
* @param Skin $skin
* @param string &$text
* @return bool
*/
public static function onSkinAfterBottomScripts( Skin $skin, &$text = '' ) {
global $wgGoogleAnalyticsAccount, $wgGoogleAnalyticsAnonymizeIP, $wgGoogleAnalyticsOtherCode,
$wgGoogleAnalyticsIgnoreNsIDs, $wgGoogleAnalyticsIgnorePages, $wgGoogleAnalyticsIgnoreSpecials;
if ( $skin->getUser()->isAllowed( 'noanalytics' ) ) {
$text .= "<!-- Web analytics code inclusion is disabled for this user. -->\r\n";
return true;
}
$ignoreSpecials = array_filter( $wgGoogleAnalyticsIgnoreSpecials, static function ( $v ) use ( $skin ) {
return $skin->getTitle()->isSpecial( $v );
} );
if ( count( $ignoreSpecials ) > 0
|| in_array( $skin->getTitle()->getNamespace(), $wgGoogleAnalyticsIgnoreNsIDs, true )
|| in_array( $skin->getTitle()->getPrefixedText(), $wgGoogleAnalyticsIgnorePages, true ) ) {
$text .= "<!-- Web analytics code inclusion is disabled for this page. -->\r\n";
return true;
}
$appended = false;
if ( $wgGoogleAnalyticsAccount !== '' ) {
$text .= <<<EOD
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', '
EOD
. $wgGoogleAnalyticsAccount . <<<EOD
', 'auto');
EOD
. ( $wgGoogleAnalyticsAnonymizeIP ? " ga('set', 'anonymizeIp', true);\r\n" : "" ) . <<<EOD
ga('send', 'pageview');
</script>
EOD;
$appended = true;
}
if ( $wgGoogleAnalyticsOtherCode !== '' ) {
$text .= $wgGoogleAnalyticsOtherCode . "\r\n";
$appended = true;
}
if ( !$appended ) {
$text .= "<!-- No web analytics configured. -->\r\n";
}
return true;
}
/**
* @param string[] &$files
* @return bool
*/
public static function onUnitTestsList( array &$files ) {
// @codeCoverageIgnoreStart
$directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/tests/' );
/**
* @var SplFileInfo $fileInfo
*/
$ourFiles = [];
foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) {
$ourFiles[] = $fileInfo->getPathname();
}
}
$files = array_merge( $files, $ourFiles );
return true;
// @codeCoverageIgnoreEnd
}
}