Why is it so difficult to add Google Analytics?

Hey guys, after the most recent theme update, Google Analytics is not being added to my site after adding the Script ID in X settings.

I don’t mind that much since I prefer to add it myself to the header.php file. So went to do that and you seem to have made it very difficult to add it manually. Why is that? I shouldn’t have to create a child theme just to manually add a script to a theme file.

This is basically the header file: x_get_view( ‘header’, ‘base’ );

What am I missing?

Hello @peasquared,

Thanks for asking. :slight_smile:

There are two way of enabling Google Analytics on your website, First is plugin and second is custom code.

  1. Plugin route: If you want to add Google Analytics via plugin route then please install and activate Google Analytics plugin from X > Validation > Extensions. Please take a look at following tutorial to get started with the plugin.


2. Custom code: The reason why we encourage users to install and use child theme (you don’t have to create child theme, we have done it for you) is because you are not directly fiddling with the core theme files. Let’s say you made the changes in the core theme files to add Google Analytics and in future you decide to update the theme. Please note that the changes you did will get overridden with the new files and at times that create lot of additional problems. To overcome the same issue, WordPress community came up with the idea of child theme. To lean more about the benefit of child theme, please take a look at the official WordPress documentation:

https://codex.wordpress.org/Child_Themes

In that regards we highly encourage yo to install and activate child theme. You can use following resources to download and setup child theme.

Download Child theme from following source: https://theme.co/apex/child-themes

Please take a look at following article to setup child theme:

After that add following code in child theme function.php file to setup Google Analytics code.

function third_party_tracking_code() { ?>

      <script>
       
      // Google Analytics.
      </script>

    <?php }

    add_action( 'wp_head', 'third_party_tracking_code' );  



Replace // Google Analytics. with the tracking code.

You also have the option to use Header and Footer plugin to add tracking code. Here are the details:

Note: Please note thatHeader and Footer plugin is not a official Themeco advice. It’s just a personal input that I have shared with you to help you get started. 3rd party plugins may cause some issues and we won’t be able to provide support in that scenario.

Thanks.

Ok, thanks for your quick reply Prasant.

You’re most welcome.

I went the Child Theme route and all is working as it should. Thanks again!

Glad hat we could be of a help :slight_smile:

Ok guys, sorry to keep this going, but I do have another question that is related.
I’m exploring the option of Google Tag Manager instead, which requires me to place a script just after the opening tag.

Since there isn’t a WP_BODY to call in the functions.php file of my child theme. How can I make sure this script is added to the Body tag?

Thanks!

Hi There,

To add the script inside the body tag, please add this code under functions.php file locates in your child theme:

function third_party_tracking_code() { ?>

      <script>
       
      // Google Analytics.
      </script>

    <?php }

    add_action( 'x_before_site_begin', 'third_party_tracking_code' );

Hope it helps :slight_smile:

Perfect, thanks again, guys!

You’re welcome!
We’re glad we were able to help you out.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.