I'm doing up a WordPress theme at the moment, and I want users to be able to add Google Analytics to their site by just adding their tracking code -- you know, the UA-20149670-1
-type number thing.
At the moment I have all the Google Analytics JavaScript code sitting in the <head>
of my pages, with the tracking code being set by the user via a theme option text input form.
However, if the user doesn't add a tracking code, then all the Google JavaScript is still left sitting in the <head>
, without a tracking code, bloating up the header.
How can I code this so that the Google Analytics code only appears after the user puts in their tracking code?
Additionally, at the moment the Google Analytics code is all still in header.php
-- ideally I'd like to move it instead to functions.php
. How best would I do that?
Thanks.