Title: Adding google analytics to functions.php
Last modified: February 16, 2020

---

# Adding google analytics to functions.php

 *  [mipqim](https://wordpress.org/support/users/yoencaz/)
 * (@yoencaz)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/)
 * 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 6 years, 3 months ago by [mipqim](https://wordpress.org/support/users/yoencaz/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadding-google-analytics-to-functions-php%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Little Package](https://wordpress.org/support/users/littlepackage/)
 * (@littlepackage)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12442984)
 * Hi [@yoencaz](https://wordpress.org/support/users/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.
 *  [Tim Burkart](https://wordpress.org/support/users/bigmoxy/)
 * (@bigmoxy)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12443010)
 * 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](https://wordpress.org/support/users/yoencaz/)
 * (@yoencaz)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12443171)
 * First of all. Crazy fast respons from you, many many thanks!
 * [@bigmoxy](https://wordpress.org/support/users/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](https://wordpress.org/support/users/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..)?
 *  [Little Package](https://wordpress.org/support/users/littlepackage/)
 * (@littlepackage)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12443219)
 * [@yoencaz](https://wordpress.org/support/users/yoencaz/)
 * I think you have us mixed up, but you’re very welcome. Actually, I liked [@bigmoxy](https://wordpress.org/support/users/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](https://wordpress.org/support/users/yoencaz/)
 * (@yoencaz)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12444141)
 * Ah Sorry!
 * [@littlepackage](https://wordpress.org/support/users/littlepackage/) and [@bigmoxy](https://wordpress.org/support/users/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 6 years, 3 months ago by [mipqim](https://wordpress.org/support/users/yoencaz/).
    -  This reply was modified 6 years, 3 months ago by [mipqim](https://wordpress.org/support/users/yoencaz/).
 *  [Little Package](https://wordpress.org/support/users/littlepackage/)
 * (@littlepackage)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12447334)
 * Hi [@yoencaz](https://wordpress.org/support/users/yoencaz/)
 * Interesting I don’t see what [@bigmoxy](https://wordpress.org/support/users/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.
 *  [Tim Burkart](https://wordpress.org/support/users/bigmoxy/)
 * (@bigmoxy)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12447370)
 * 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
 *  [Little Package](https://wordpress.org/support/users/littlepackage/)
 * (@littlepackage)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12447386)
 * ” > Theme Settings > ” is the giveaway that it’s specific to a theme. 🙂
 *  Thread Starter [mipqim](https://wordpress.org/support/users/yoencaz/)
 * (@yoencaz)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12447458)
 * 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.

## Tags

 * [child theme](https://wordpress.org/support/topic-tag/child-theme/)
 * [function.php](https://wordpress.org/support/topic-tag/function-php/)
 * [header.php](https://wordpress.org/support/topic-tag/header-php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [mipqim](https://wordpress.org/support/users/yoencaz/)
 * Last activity: [6 years, 3 months ago](https://wordpress.org/support/topic/adding-google-analytics-to-functions-php/#post-12447458)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
