analytics.php
1.72 KB
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
<?php
/**
* This is the output for google analytics on the page.
*
* @since 4.0.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
// phpcs:disable Generic.WhiteSpace.ScopeIndent.Incorrect
// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
$googleAnalyticsId = aioseo()->options->deprecated->webmasterTools->googleAnalytics->id;
if ( empty( $googleAnalyticsId ) ) {
return;
}
$options = $this->analytics->getOptions();
?>
<script type="text/javascript"<?php echo $this->analytics->getScriptAttributes(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', <?php echo wp_json_encode( $googleAnalyticsId ); ?><?php echo wp_kses_post( $options['domain'] ); ?><?php echo wp_kses_post( $options['jsOptions'] ); ?>);
<?php
foreach ( $options['options'] as $option ) :
$string = 'ga(';
foreach ( $option as $o ) :
$string .= is_bool( $o )
? $o
: (
is_array( $o )
? '[\'' . sanitize_text_field( implode( '\', \'', $o ) ) . '\']'
: '\'' . sanitize_text_field( $o ) . '\', '
);
endforeach;
$string = rtrim( trim( $string ), ',' ) . ");\n";
?>
<?php echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endforeach; ?>
ga('send', 'pageview');
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
<?php if ( $this->analytics->autoTrack() ) : ?>
<script async src="<?php echo esc_url( add_query_arg( 'ver', AIOSEO_VERSION, $this->analytics->autoTrackUrl() ) ); ?>"></script>
<?php endif; ?>