2

I have a javascript code (like Google Analytics Code) that I want to use in my Wordpress blog. Is there any way that I could directly do it? I did some googling and found that due to security reasons, wordpress won't allow to do it directly.

Can I write a plugin that installs a JS code on a wordpress blog?

Thank You.

3
  • You can add JS through POST and PAGE. Commented Mar 15, 2012 at 12:45
  • That would be like pasting code every time? for each post/page? I don't want to do that. I want it to appear throughout the website by adding the code somewhere once. Commented Mar 15, 2012 at 12:48
  • You can add your JS in theme.. Isn't it proper way for you ? Commented Mar 15, 2012 at 12:52

3 Answers 3

5

There is no security risk to modify any theme if you know what you are exactly doing and themes are made by developers and you can customize it.

function add_ga_code() 
{
?>
<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']); //Update 'UA-XXXXX-X' with valid account id
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>
<?php
}
add_action('wp_head', 'add_ga_code');

Add this in your functions.php file located in your theme folder or just put the whole script between head tag before wp_head() of your header.php file which is also located inside your theme folder.

Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that it is better to place analytics just before </body>. And you should do it this way, and not put it directly in header.php or footer.php
1

I'd put the code in the theme header.php file:

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']); //Update 'UA-XXXXX-X' with valid account id
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

Before the </head> tag.

Rob

Comments

0

The best way to handle this is to add the code provided by Google to the header.php. Add this before the wp_head() call. This will load Google Analytics before any other enqueued scripts. Because Google Analytics loads asynchronously, it will not hold up the next script in line from loading. Also any enqueued scripts that cause JS errors will not stop Analytics from tracking. This is good if you have unreliable plug ins.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.