Viewing 15 replies - 1 through 15 (of 17 total)
  • Hi there,

    this article provides 2 PHP snippets for adding Gtag codes:

    https://docs.generatepress.com/article/implement-google-tag-manager/#using-functions

    Thread Starter masterhealerwp

    (@masterhealerwp)

    Thanks.

    I’m not a coder and this is the first time I set a blog.
    I read the link you provided.

    2 questions:

    1. When I go to Appearance, I see no Elements option. Please note my GUI is in French. I see one option called Widgets. Is it the one I should click?

    2. In the PHP code, I see this: GTM-XXXX
    Should I replace the XXX with the PROPERTY ID I can find in Google Analytics?

    Thank you

    ying

    (@yingscarlett)

    1. Elements is a GP premium feature, it’s not included in the free theme.

    2. Google analytics should give you the entire code, you just need to replace the code between <!-- Google Tag Manager --> and <!-- End Google Tag Manager -->.

    Thread Starter masterhealerwp

    (@masterhealerwp)

    2. Where should I put all this?

    In functions.php of my GeneratePress Child Theme?

    ying

    (@yingscarlett)

    Yes, correct!

    Thread Starter masterhealerwp

    (@masterhealerwp)

    I followed this tuto but when copying this code snippet:

    /*TO ADD GOOGLE ANALYTICS*/
    <?php
    add_action('wp_head', 'pn_add_analytics');
    function pn_add_analytics() { ?>
    
    <!--Paste your Google Analytics code here-->
    
    <?php } ?>

    I do have that error:
    syntax error, unexpected '<', expecting end of file

    Hi @masterhealerwp,

    There’s a syntax error coming from the php opening or closing tags.

    Can you copy the last 5 lines before and after where you’re adding this code?

    Separate them through 2 code blocks.

    Hope to hear from you soon.

    Thread Starter masterhealerwp

    (@masterhealerwp)

    Ok, here is my full functions.php:

    <?php
    /**
     * GeneratePress child theme functions and definitions.
     *
     * Add your custom PHP in this file.
     * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
     */
    
     /*To add the date of the day in the "byline" of the posts*/
    add_action('wp', function() {
    	 if (is_single()) {
    		add_filter( 'generate_post_date_output', 'remove_post_date',$time_string, $posted_on);
    	}
    });
    function remove_post_date() {
    	return;
    }
    
    add_action('generate_after_entry_title', function() {
    	if(is_single()) {
    	    $custom_updated_date = date(get_option('date_format'));
    	    echo '<div class= "last-updated-date">Article mis à jour le '.$custom_updated_date.'</div>';
    	}
    	return; 
    }, 11, 10);
    
    /*TO ADD GOOGLE ANALYTICS*/
    <?php
    add_action('wp_head', 'pn_add_analytics');
    function pn_add_analytics() { ?>
    
    <!--Paste your Google Analytics code here-->
    
    <?php } ?>
    ying

    (@yingscarlett)

    You just need to remove ?> in the end of the below code:

    /*TO ADD GOOGLE ANALYTICS*/
    <?php
    add_action('wp_head', 'pn_add_analytics');
    function pn_add_analytics() { ?>
    
    <!--Paste your Google Analytics code here-->
    
    <?php } ?>
    • This reply was modified 2 years, 5 months ago by ying.
    Thread Starter masterhealerwp

    (@masterhealerwp)

    I’m really turning crazy with this.

    I tried as you said, while removing the final ?> :

    /*TO ADD GOOGLE ANALYTICS*/
    <?php
    add_action('wp_head', 'pn_add_analytics');
    function pn_add_analytics() { ?>
    
    <!--Paste your Google Analytics code here-->
    
    <?php }

    I still have this error message:
    syntax error, unexpected ‘<‘, expecting end of file

    I see.

    Try this:

    <?php
    /**
     * GeneratePress child theme functions and definitions.
     *
     * Add your custom PHP in this file.
     * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
     */
    
     /*To add the date of the day in the "byline" of the posts*/
    add_action('wp', function() {
    	 if (is_single()) {
    		add_filter( 'generate_post_date_output', 'remove_post_date',$time_string, $posted_on);
    	}
    });
    function remove_post_date() {
    	return;
    }
    
    add_action('generate_after_entry_title', function() {
    	if(is_single()) {
    	    $custom_updated_date = date(get_option('date_format'));
    	    echo '<div class= "last-updated-date">Article mis à jour le '.$custom_updated_date.'</div>';
    	}
    	return; 
    }, 11, 10);
    
    /*TO ADD GOOGLE ANALYTICS*/
    
    add_action('wp_head', 'pn_add_analytics');
    function pn_add_analytics() {
    
        /*Paste your Google Analytics code here*/
    }
    

    Kindly let us know how it goes!

    Thread Starter masterhealerwp

    (@masterhealerwp)

    Thank you.

    It sill does nto work.

    Here is what I pasted from you, adding the Google script:

    /*TO ADD GOOGLE ANALYTICS*/
    add_action('wp_head', 'pn_add_analytics');
    function pn_add_analytics() {
        /*Paste your Google Analytics code here*/
    		<!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-K3Q6SRXERT"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'G-K3Q6SRXERT');
    </script>

    Here is what I have when I try to save:
    syntax error, unexpected ‘<‘ on line 33.
    Line 33 is: <!–Global site tag (gtag.js) – Google Analytics–>

    Hi there,

    try this PHP Snippet instead:

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

    I copied the script from your code, please make sure it matches your actual gtag script.

    Thread Starter masterhealerwp

    (@masterhealerwp)

    Wow, no more error.
    I implemented it thanks to you.
    Let’s wait a few days to see how it goes.

    You’re welcome

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Best way to install Google Analytics with GeneratePress?’ is closed to new replies.