• Hello, if someone could take time and answer this question I would be thankful! ??

    Goal:
    Add google analytics to my childtheme.

    Question:
    1.What code do I add in my functions.php? I found the code snippet down below:

    add_action('wp_head','my_analytics', 20);
    function my_analytics() {
    	?>
                YOUR GOOGLE ANALYTICS TRACKING CODE HERE
                (looks like <script type="text/javascript">.....</script> )
    	<?php
    }

    Would this code above be enough?

    2. If you think it is better to add google analytics in the child themes header.php I wonder: How is the parent-theme-header.php file related to the the child-theme-header.php? Does the child-theme-header.php need to have the exact same code as the parent-theme-header.php (except from the code added), or could it just contain the additional code snippets?.

    • This topic was modified 4 years, 9 months ago by mipqim.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @yoencaz

    1) At quick glance the first code looks fine, I’d correct it a little to:

    add_action( 'wp_head', 'my_own_analytics', 20 );
    function my_own_analytics() { ?>
        // YOUR GOOGLE ANALYTICS TRACKING CODE HERE
        // technically it doesn't have to say type="text/javascript", just <script>
        <script type="text/javascript">.....</script>
    <?php
    }

    2) You could do #1 or #2. If your parent theme has breaking updates frequently though, #2 might be a pain in the rear. Anyway, to do #2, take the parent theme header.php, copy it “word-for-word” to your child theme folder, and add the line of Google Analytics code where Google suggests. Yes, it needs to be essentially just like the parent header.php, except with your careful edits.

    Wouldn’t it be simpler to add your GA code to in the theme customizer? There is a section for adding code to the header and footer of your theme.

    What child theme are you using?

    Thread Starter mipqim

    (@yoencaz)

    First of all. Crazy fast respons from you, many many thanks!

    @bigmoxy
    1) Are you saying I could add the code exactly the way I present it down below (except replacing the stars with my id) to my functions.php in my child theme?

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

    Also, regarding changes in the functions.php file: In general, can I feel safe that if anything goes wrong during new changes, I can always return the code to what it was, and all things should be back to normal again? Just want to understand if I could have kind of a “BOLD MENTALITY” –> change, error, change back, change, success etcetera…..?

    Last thing, are you saying that adding GA to functions.php is to prefer (I have storefront parent theme and storefront child theme, if that info changes anything)?

    @littlepackage
    I use storefront parent theme and storefront child theme. I thought wordpress customizer was related to the parent theme, so that when updates are made any changes might get lost?
    Anyhow I think I prefer to have a structure where I do changes directly in the files, so that I know exactly where my code is (perhaps this is not the best..)?

    @yoencaz

    I think you have us mixed up, but you’re very welcome. Actually, I liked @bigmoxy’s recommendation regarding the Customizer, and would probably do that if I were you. Customizer settings should be retained during updates.

    That said, you’re asking about playing with the functions.php file. If your site is live, but you don’t mind it going down for a minute or two here and there, go ahead and edit that file (in a child theme). Make a backup so you can revert to the working version of functions.php if your changes go wrong. This is a great way to learn some coding, because the PRESSURE IS ON to get it right!

    In general, can I feel safe that if anything goes wrong during new changes, I can always return the code to what it was, and all things should be back to normal again?

    In short, yes!

    Thread Starter mipqim

    (@yoencaz)

    Ah Sorry!

    @littlepackage and @bigmoxy
    Ok I do as you recommend!
    But I am not quite sure what you mean with the customizer because:

    When I go to my customizer, the only option where I can put in code is under “additional css”. The “header” and “footer section” only have color options.
    Are you perhaps talking about the “theme editor section”? If you are, then my problem is that I only have three files (for my child theme) —> style.css, functions.php and README.md.

    So where exactly are you saying I should add GA?

    • This reply was modified 4 years, 9 months ago by mipqim.
    • This reply was modified 4 years, 9 months ago by mipqim.

    Hi @yoencaz

    Interesting I don’t see what @bigmoxy is referring to, either. I’ve checked out a couple themes and don’t see it. Maybe go ahead and use the functions.php method or edit your child theme header.php file.

    OK, I’m sorry everyone! Maybe the option to add code to the header / footer is specific to Genesis themes which I have been using for some time. But here is how I get to that option:

    Appearance > Customize > Theme Settings > Header/Footer Scripts

    ” > Theme Settings > ” is the giveaway that it’s specific to a theme. ??

    Thread Starter mipqim

    (@yoencaz)

    Great! ??

    I have played around with functions.php and perceive it being more ”sensitive” to changes than header.php, so I choose the header.php option.

    Regarding my choise:
    I cannot find any info about the relationship between the parent theme-header.php and the child theme-header.php file?
    Right now its a bit unclear to me how wordpress prioritize the code in these files. For example. Lets say I now copy my parent theme-header file to my child theme and add GA to it.
    What happens when storfront (parent theme) gets updated?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Adding google analytics to functions.php’ is closed to new replies.