Google Analytics Setup on the Sydney Child Theme

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

My website link is this: https://rb.gy/tna9a2

Hello there,

Thank you for contacting us about out theme Sydney. I am happy to help out.

We have some plugins recommendation that you can choose for that purpose.

Have you tried plugin method? What is blocking you from using a plugin?

Regards,
Kharis
aThemes Support

Dear Kharis,

We do not want to do it with a plugin, I explained it above.
We don’t want to slow our website which already have dozens of active plugins installed. So we want to integrate the code directly. Can you help us to direct where exactly should we paste our tracking code on the functions.php

Can you help here?

Hello there,

Please share your child theme’s functions to https://pastebin.com/ and update me with the the to it. So, I can make some corrections.

Regards,
Kharis
aThemes Support

Dear Karis,

Link: https://pastebin.com/TZRjiHEZ
Password: Kharis-aThemes

Thanks a lot

Haven’t added the google tracking code on the functions.php

Below is the code that should be added to the functions.php document

<?php
add_action(‘wp_head’, ‘wpb_add_googleanalytics’);
function wpb_add_googleanalytics() { ?>
"Our Google Analytics tracking code" (gtag)

Hello there,

Thank you for sharing the link.

Can you also share you Google Analytics tracking code in your reply to this reponse? So I can just copy and paste it and avoid you put it into incorrect place.

Regards,
Kharis
aThemes Support

Dear Kharis,

I can not share the whole code since its not safe to do so, I am deleting our account id on the code. And instead add XXXXs on the id.

    Code: < !-- Global site tag (gtag.js) - Google Analytics -->
    < script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"> 
    < /script>
    < script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXX-X');
    < /script>

I believe in order to add this code to functions.php we must include the following code as well:

    <?php
    add_action('wp_head', 'wpb_add_googleanalytics');
    function wpb_add_googleanalytics() { ?>
     
    // Paste your Google Analytics tracking code from Step 4 here
     
    <?php } ?>

So the final code should be as follows:

    <?php
    add_action('wp_head', 'wpb_add_googleanalytics');
    function wpb_add_googleanalytics() { ?>
     
    <!-- Global site tag (gtag.js) - Google Analytics -->
    < script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X">
    < /script>
    < script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXX-X');
    < /script>
     
    < ?php } ?>

Note: There shouldn’t be space between < / which is written on < /script>
If I write them correctly it doesn’t show on the message

Hello there,

Thank you for sharing.

Here is the corrected code: https://pastebin.com/PzZs72Bq. Use this password below to unlock.

CF7SYaZpFs

You need to replace UA-XXXXXXXX-X with your tracking ID.

I hope this helps. And let me know your views.

Regards,
Kharis
aThemes Support

1 Like

Dear Kharis,

Thanks a lot, it works!

So now we won’t lose our tracking code whenever we update the theme since we added the code on our child theme is that right?

Best Regards

1 Like

You’re welcome!

Absolutely correct!

Please let us know in a new topic if you have any further questions, or if we can provide you with any other assistance.

Regards,
Kharis
aThemes Support