Hello,
Whenever I update Sydney Theme the Google Analytics tracking code is lost, so I am trying to add my tracking code on the Sydney Child Theme > Theme Functions (functions.php) folder.
Question 1: Is this the right place to add the analytics code so it won’t be deleted after any Theme updates?
So I had some critical issues after trying this myself which I share my code below:
1st Try
Error: Website is Broken - There has been a critical error on your website. Please check your site admin email inbox for instructions. Can’t open the website, it gives the above error
Code: <?php
// Exit if accessed directly
if ( !defined( ‘ABSPATH’ ) ) exit;
//ADDED CODE THAT CAUSES THE ERROR
<?php
add_action(‘wp_head’, ‘wpb_add_googleanalytics’);
function wpb_add_googleanalytics() { ?>
"Our Google Analytics tracking code"
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( ‘chld_thm_cfg_locale_css’ ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . ‘/rtl.css’ ) )
$uri = get_template_directory_uri() . ‘/rtl.css’;
return $uri;
}
endif;
add_filter( ‘locale_stylesheet_uri’, ‘chld_thm_cfg_locale_css’ );
if ( !function_exists( ‘chld_thm_cfg_parent_css’ ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( ‘chld_thm_cfg_parent’, trailingslashit( get_template_directory_uri() ) . ‘style.css’, array( ‘sydney-wc-css’,‘sydney-bootstrap’ ) );
}
endif;
add_action( ‘wp_enqueue_scripts’, ‘chld_thm_cfg_parent_css’, 10 );
// END ENQUEUE PARENT ACTION
Code ENDS HERE
2nd Try
Error: This actually works the Google tracking code in which I can track the traffic on my site after adding the following lines, but it shows “END ENQUEUE PARENT ACTION” on the top left side of the website which shouldn’t be there
Code: <?php
// Exit if accessed directly
if ( !defined( ‘ABSPATH’ ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( ‘chld_thm_cfg_locale_css’ ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . ‘/rtl.css’ ) )
$uri = get_template_directory_uri() . ‘/rtl.css’;
return $uri;
}
endif;
add_filter( ‘locale_stylesheet_uri’, ‘chld_thm_cfg_locale_css’ );
if ( !function_exists( ‘chld_thm_cfg_parent_css’ ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( ‘chld_thm_cfg_parent’, trailingslashit( get_template_directory_uri() ) . ‘style.css’, array( ‘sydney-wc-css’,‘sydney-bootstrap’ ) );
}
endif;
add_action( ‘wp_enqueue_scripts’, ‘chld_thm_cfg_parent_css’, 10 );
// ADDED CODE THAT CAUSES THE ERROR
<?php
add_action(‘wp_head’, ‘wpb_add_googleanalytics’);
function wpb_add_googleanalytics() { ?>
"Our Google Analytics tracking code"
// END ENQUEUE PARENT ACTION
Code ENDS HERE
If I add php closing tag <?php } ?> on the bottom part of the code line It won’t save or run the functions.php document.
Question 2 : So I believe I am doing something wrong here, can you please help me out to fix this issue to run the Google analytics code on my site’s child theme? I don’t prefer using any plugins since we already running bunch of them which slows our website.
Documents on my Child Theme :
- Stylesheet ( style.css )
- Theme Functions ( functions.php )
- Theme Footer ( footer.php )
Thank you in advance!
Best Regards,
Ahmet