Code Typo In Google Analytics Demographics and Interests Reports Snippet
Ah. The old apostrophe that looks like a single quote catastrophe all code monkeys dread.
Warning! If you have it on your to-do list to get your Google Analytics account up and running with the new Google Analytics Demographics and Interests reports be aware that there is a sneaky apostrophe in the DC.js code snippet supplied on the Enable Demographics and Interests Google Analytics Help page that should be a single quote.
To get demographic and interests data flowing into your GA account, Google Analytics Help instructs you to:
Change:
ga.src = (‘https:’ == document.location.protocol ?’https://ssl’ : ‘http:// www’) + ‘google-analytics.com/ga.js’;
To:
ga.src = (‘https:’ == document.location.protocol ? ‘https://’ : ‘http://’) + ‘stats.g.doubleclick.net/dc.js’;
Note to innocent copy-pasters that the ‘ before ‘stats.g.doubleclick.net/dc.js’; is typed as an apostrophe on the help page (and here because WordPress keeps changing the HTML…) and needs to be changed to a single quote in order to avoid a frustrating GA crash and a long-winded troubleshooting session.
Questions? Ask away in the comments section!
6 Replies to “Code Typo In Google Analytics Demographics and Interests Reports Snippet”
For those of us using Universal Analytics – the code will not allow for demographic data – anyone know how to fix this?
Here is a sample of Universal Analytics code that will not validate for the demographic data.
(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-12345678’, ‘yourdomain.com’);
ga(‘send’, ‘pageview’);
just an update…
the business with ‘.’ – your article is correct.
also, after trolling around and trying a few things, i found a 30oct2013 link that makes me think this is all a bit early:
http://productforums.google.com/forum/#!topic/analytics/4hQJLrojd38
Thanks for your article. I looked on Yoast but didn’t find anything mentioning this, so far.
Here’s the real plugin code [below] – a bit more involved than the example.
Isn’t there a missing ‘.’ in ‘google-analytics.com/ga.js’?
Should read ‘.google-analytics.com/ga.js’?
And correspondingly, should
‘stats.g.doubleclick.net/dc.js’ read
‘.stats.g.doubleclick.net/dc.js’?
Do you happen to know ramifications (if any) for current_user_can ‘manage_options’ in the suggested changes for demographics? Is the value ‘u/ga_debug.js’ still a valid $script value?
(function () {
var ga = document.createElement(‘script’);
ga.type = ‘text/javascript’;
ga.async = true;
ga.src = options[‘gajsbruceclaying’] && !empty( $this->options[‘gajsurl’] ) )
{echo “‘” . $this->options[‘gajsurl’] . “‘;”; } else {
$script = ‘ga.js’;
if ( current_user_can( ‘manage_options’ ) && $this->options[‘debug’] )
$script = ‘u/ga_debug.js’;
echo “(‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/” . $script . “‘”;}
?>;
var s = document.getElementsByTagName(‘script’)[0];
s.parentNode.insertBefore(ga, s);
})();
Hi Chelsea, Thanks for the suggestions. I am running analytics code via “Analytics for WordPress” plugin by Joost. However, there is no option for the code as you mentioned here to update via “Analytics for WordPress” plugin. Do you have any idea how to do that? Thanks in advance.
Hi, Abinash!
Questions:
1) When you go into Google Analytics, can you see data in the Demographics left column pulldown? If you can, that means that Joost has automatically updated the HTML in your plugin and you don’t have to worry about adding the code snippet mentioned in this blog post. You’re good to go.
2) If you don’t see demographic metrics being pulled into the Demographics portion of your GA account that means you need help from Joost… or someone who knows how to update code.
With this scenario you have two options:
a) Email Joost and ask him to help you! (Like this guy did: https://github.com/Yoast/google-analytics-for-wordpress/issues/6)
b) Do it yourself like this:
(Based on the instruction for inserting AdWords Retargeting in this blog post: http://www.boom-online.co.uk/adwords-remarketing-with-wordpress-and-yoasts-google-analytics-plugin/#axzz2j2IwYHd3)
Go to the Plugins > Editor section of your WordPress admin and choose Google Analytics for WordPress from the “Select plugin to edit:” menu on the right.
From the list of files that appears below, click “google-analytics-for-wordpress/frontend/class-frontend.php”.
Use the browser’s find function to search for this code snippet
ga.src = (‘https:’ == document.location.protocol ?’https://ssl’ : ‘http:// www’) + ‘google-analytics.com/ga.js’;
Replace that snippet with this snippet:
ga.src = (‘https:’ == document.location.protocol ? ‘https://’ : ‘http://’) + ‘stats.g.doubleclick.net/dc.js’;
and update the file.
NOTE! IF YOU ARE NOT AWESOME AT CODE THERE IS A CHANCE YOU COULD BREAK THE PLUGIN IF YOU GO CHANGING THINGS WILLY NILLY….. SO PLEASE ENTER WITH CAUTION! For real. Email Joost first and ask before taking matters into your own hands if you don’t know what you’re doing because it is really, really inconvenient to have your GA crash.
Any word on how to collect demographics and interest data for sites using Universal Analytics? (https://support.google.com/analytics/answer/2790010)