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
(@merylu)
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 ?
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
}
@qxare
I would put it in a wp-content/plugins/ga-plugin.php or something instead.
@merylu
Lightweight enough for you? π
Thread Starter
Merylu
(@merylu)
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
(@merylu)
Sorry Rolling Wolf but I don’t understand your answer.
I don’t have a folder called ga-plugin.php
@merylu: Use a plugin; it won’t slow down your site: https://ww.wp.xz.cn/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
(@merylu)
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 .
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
(@merylu)
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>
}
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;