there, I was wondering if there's a way to place the Google

Hello there,

I was wondering if there's a way to place the Google Analytics in more customizable places.

We don't want to use before/after page/posts; what kind of hook/code can we add to our theme template file to achieve this?

  • Ash

    Hello @in-mn

    I hope you are well today and thanks for asking the question.

    You can try the following:

    add_action('wp_head', 'google_analytics');
    function google_analytics() {

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always.

    Hope it helps! Please feel free to ask more question if you have.


  • Maniu

    Hello @in-mn

    You should be able to remove tracking code by adding this code to functions.php file you want it removed from:

    global $google_analytics_async;
    remove_action( 'wp_head', array( $google_analytics_async, 'tracking_code_output' ); );

    Also you can use the same code to remove GA from head of all sites (by placing this code as mu-plugin) and then add it somewhere else with this:
    add_action( 'wp_footer', array( $google_analytics_async, 'tracking_code_output' );

    Sorry for late replay, notification about it went to spam:slight_frown: