Adding Google Analytics to functions.php

Hi guy, can anybody tell me how to add the google analytics code (or other tracking scripts) to the functions.php file in PRO theme please?

I looked around the forum and saw some solutions but they didnโ€™t seem to work for me with the live site check even after flushing caches etc.

Hello @Naishster,

Thanks for asking. :slight_smile:

Please add following code in child theme function.php file:

add_action('wp_head', 'wpb_add_googleanalytics');
function wpb_add_googleanalytics() { ?>

// Paste your Google Analytics code
<!-- Global site tag (gtag.js) - Google Analytics -->


<?php } ?>

Please make sure to install and setup child theme. I am sharing the relevant resources that you can use to install and setup child theme.

https://theme.co/apex/child-themes

You also have the option to use plugins like Insert Headers and Footers to add Google Analytics plugin. In case you are interested, please take a look at following source.

Thanks.

Would prefer no plugins to be honest. :slight_smile:

That code was causing an error on the line beginning <?php

Also, I have child theme in place thanks.

Sorry, just wanted to make sure you saw the last as I see you updated the above since. :slight_smile:
Can you please confirm the code was correct as I have tried a few times and it keeps causing an error and the file rolls back.

Hereโ€™s the error msg:

Your PHP code changes were rolled back due to an error on line 81 of file wp-content/themes/pro-child/functions.php. Please fix and try saving again.

syntax error, unexpected โ€˜<โ€™, expecting end of file

Hello @Naishster,

Thanks for updating the thread. I am really sorry for the same. Actually there was a typo error in the code that I have fixed. Please try out the code I shared in previous reply.

Apologies for the error.

Thanks.

Brilliant, job done thanks @Prasant. :slight_smile: