Title: Installing google analytics
Last modified: August 24, 2016

---

# Installing google analytics

 *  [Merylu](https://wordpress.org/support/users/merylu/)
 * (@merylu)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/)
 * I read your explanations in google analytics Installing forum but can not find
   answer to my problem . The code assumes that you have to stick in your theme 
   header.php folder up, there all right . You say that you have to stick just before
   < / head> , what if I can not find it ? and instead what appears to me is < /
   header> fitting it before this? equally ? It is the same?
    I wait for your help,
   thanks.

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

 *  [rollingWolf](https://wordpress.org/support/users/rollingwolf/)
 * (@rollingwolf)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165122)
 * I wouldnt recommend you putting it in header.php at all but in footer.php just
   before </body></html>. There are several plugins though that adds the necessary
   code for you if you just provide the tracking ID if you dont want to do it yourself.
 * Another reason for using a plugin instead of hacking the themefiles yourself (
   unless youve made the theme) is because as soon as you update the theme the changes
   will be lost.
 *  Thread Starter [Merylu](https://wordpress.org/support/users/merylu/)
 * (@merylu)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165130)
 * Thanks RollingWolf but my idea is not to use pluggin since I installed a few 
   and that slows down the speed of the web , and I would like if possible to solve
   this without a pluggin .
    I have a theme son, my idea is to put the code in the
   subject to avoid having to make changes and modifications each time you update
   it. You can help me ?
 *  [QXARE](https://wordpress.org/support/users/qxare/)
 * (@qxare)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165134)
 * As Google recommends to put the new analytics code right in front of the `</head
   >` tag you can add something like this in your `functions.php`:
 *     ```
       add_action('wp_head', 'google_analytics_code');
       function google_analytics_code() {
         // echo JS code
       }
       ```
   
 *  [rollingWolf](https://wordpress.org/support/users/rollingwolf/)
 * (@rollingwolf)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165136)
 * [@qxare](https://wordpress.org/support/users/qxare/)
 * I would put it in a wp-content/plugins/ga-plugin.php or something instead.
 * [@merylu](https://wordpress.org/support/users/merylu/)
 * Lightweight enough for you? 🙂
 *  Thread Starter [Merylu](https://wordpress.org/support/users/merylu/)
 * (@merylu)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165161)
 * Thanks Qxare
    Can you explain to me what I get if I put this code in functions.
   php And after put this code in functions, which it is supposed to have to do 
   next, which would be the next step with the snippet of Analitycs google ?
 *  Thread Starter [Merylu](https://wordpress.org/support/users/merylu/)
 * (@merylu)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165164)
 * Sorry Rolling Wolf but I don’t understand your answer.
    I don’t have a folder
   called ga-plugin.php
 *  [Mark Ratledge](https://wordpress.org/support/users/songdogtech/)
 * (@songdogtech)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165170)
 * [@merylu](https://wordpress.org/support/users/merylu/): Use a plugin; it won’t
   slow down your site: [https://wordpress.org/plugins/ga-google-analytics/](https://wordpress.org/plugins/ga-google-analytics/)
   Making your own plugin is a waste of time.
 * Adding the Google Analytics code to your theme’s function file will result in
   the code beign deleted with a theme update.
 *  Thread Starter [Merylu](https://wordpress.org/support/users/merylu/)
 * (@merylu)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165175)
 * Thanks songdogtech but I work with a child theme with updates to not delete anything.
   
   if no solution gives me I will have no choice but to resort to a pluggin .
 *  [QXARE](https://wordpress.org/support/users/qxare/)
 * (@qxare)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165203)
 * Hi MeryLu, just replace the line `// echo JS code` with this
    `echo '<script>...
   </script>';` (your Google Analytics code) and you should be able to see it in
   the source code. That’s all.
 *  Thread Starter [Merylu](https://wordpress.org/support/users/merylu/)
 * (@merylu)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165205)
 * So should I put this code in the folder functions.php of my theme?
    In wich part
   of functions.php should I paste it? Thanks
 * add_action(‘wp_head’, ‘google_analytics_code’);
    function google_analytics_code(){
   <script> (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){(
   i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,
   m) })(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);
 *  ga(‘create’, ‘UA-63376405-1’, ‘auto’);
    ga(‘send’, ‘pageview’);
 * </script>
    }
 *  [QXARE](https://wordpress.org/support/users/qxare/)
 * (@qxare)
 * [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165214)
 * You can put it wherever you want, but you have to echo the JS code as mentioned
   before:
 *     ```
       function google_analytics_code() {
       	echo '<script>
       	(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
       	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
       	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
       	})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
   
       	ga("create", "UA-63376405-1", "auto");
       	ga("send", "pageview");
   
       	</script>';
       }
       ```
   
 * I have replaced the ‘ with ” to echo the code with ‘ to prevent unnecessary escaping
   of all the strings inside the JS;

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

The topic ‘Installing google analytics’ is closed to new replies.

## Tags

 * [google analitycs](https://wordpress.org/support/topic-tag/google-analitycs/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 4 participants
 * Last reply from: [QXARE](https://wordpress.org/support/users/qxare/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/installing-google-analytics-7/#post-6165214)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
