Where should I place Google analytics code in my master page?
right before </body> tag to prevent slowing download js scripts from blocking page content.
More details: http://developer.yahoo.com/performance/rules.html#js_bottom
UPDATE (2018-10-22):
Just after the <head> tag:
https://support.google.com/analytics/answer/1008080?hl=en
ORIGINAL ANSWER:
Just before the </head> tag.
https://support.google.com/analytics/bin/answer.py?hl=en&answer=1008080
If you prefer to put the Analytics snippet at the bottom of the page, you should know that you don't have to put the whole snippet at the bottom. You can still keep most of the benefits of asynchronous loading by splitting the snippet in half—keep the first half at the top of the page and move the rest to the bottom. Because the first part of the tracking snippet has little to no effect on page rendering, you can leave that part at the top and put the part of the snippet that inserts ga.js at the bottom.
A page with the asynchronous snippet split in half might look like this:
<html>
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
</script>
</head>
<body>
<p>Page Content</p>
<script src="some_random_script.js"></script>
<p>Page Content</p>
<script type="text/javascript"> (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>
</body>
</html>
Both pieces of code need to be wrapped in their own script tags, but only the last six lines of the original asynchronous snippet need to be moved to the bottom. All the lines that push methods onto _gaq can stay at the top.
Related
I am trying to get tracking in place and was wondering what will be the best place to add the JS code. Also need to make sure that is it not getting minified?
I would suggest extending outerLayout.html in lib/modules/apostrophe-templates/views/outerLayout.html. In this template, you can modify the extraHead block like so:
{% extends "outerLayoutBase.html" %}
{% block extraHead %}
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '...']);
_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>
{% endblock %}
This is the block where I typically put things like favicon definitions, open graph meta tags, remote font loading, and all that good stuff.
FYI - may be a good idea to do the analytics ID (etc) as a global so your user can set it there self. Note that this will impact performance a little as it'll be requested for each page, but it can be really useful. And don't forget to wrap the script in {% if data.global.analyticsID %}!
http://noblue.co.uk/blog/cloud-help-company-adapt-changing-working-environment/
The post is alwaying sharing wrong image to google plus. You can see the error in http://screencast.com/t/TBsgSGfopM
How we rectify this? I am using following code:
<div class="google_share">
<div class="g-plusone" data-size="tall"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
It's not an problem by google, it's a problem by your side.
Google search for a image and found it: The popup background (http://noblue.co.uk/wp-content/uploads/2014/02/popup_case.png)
You must explicit add Meta informations in the header (Open-Graph) of your Site with following informations:
<meta property="og:image" content="..." />
Look at your source, you adding already a meta, duplicate it and change the duplicat extra for google:
<meta itemprop="image" content="http://noblue.co.uk/wp-content/uploads/2014/04/Advantages-of-cloud-computing-100x80.jpg">
EDIT:
Forgot 'em! You must add for Open Graph a additional Reference to your HTML-Tag:
<html prefix="og: http://ogp.me/ns#">
One ad unit just stopped to work and after trying to generate new code it is still not working. When I use google console it is just showing warning Ad unit failed to fetch. Other 3 ads are working normally. They are inserted before I started to work on this project.
New ad code is generated like
header
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.defineSlot('/6000854/R300x250_L', [300, 250], 'div-gpt-ad-1367703773182-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
body
<!-- R300x250_L -->
<div id='div-gpt-ad-1367703773182-0' style='width:300px; height:250px;'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1367703773182-0'); });
</script>
</div>
Old code is generated like
header
<script type='text/javascript' src='http://partner.googleadservices.com/gampad/google_service.js'></script>
<script type='text/javascript'>
GS_googleAddAdSenseService("ca-pub-2838961657718357");
GS_googleEnableAllServices();
</script>
<script type='text/javascript'>
GA_googleAddSlot("ca-pub-2838961657718357", "Top728x90");
GA_googleAddSlot("ca-pub-2838961657718357", "Right300x250");
GA_googleAddSlot("ca-pub-2838961657718357", "Right300x250Bottom");
</script>
<script type='text/javascript'>
GA_googleFetchAds();
</script>
body
<!-- Right300x250Bottom -->
<script type='text/javascript'>
GA_googleFillSlot("Right300x250Bottom");
</script>
Is there maybe conflict because of diferent tags or code
NOTE: I am just wordpress front end developer, I don't have access to google DFP services and I am not generating tags or code
EDIT: Based on this discussion it looks like using the old GAM tags with GPT tags may now be an issue (just recently) http://productforums.google.com/forum/#!topic/dfp/snK7znwUMBE
I think you should convert the GAM tags to to GPT tags and use just the DFP GPT tags if possible to stop this happening... its most probably a conflict between the two scripts and depending on the order they get loaded the GPT tags may not work...
ORIGINAL:
I just tried your code and two ads showed as expected. So there does not appear to be any conflicts using both scripts at the same time.
Potentially you have been refreshing the page so many times that there are no ads that will be displaying for you through DFP anymore because of rate limits? That is just a wild guess though. If I refresh the page a lot I do get the failed to fetch message every now and then in the console so I think it is probably just a rate limit and something that a normal user will not have a problem with.
This is the code that I am using, two 300x250 ads show on the page so everything is working correctly.
<html>
<head>
<title>DFP TEST</title>
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.defineSlot('/6000854/R300x250_L', [300, 250], 'div-gpt-ad-1367703773182-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
<script type='text/javascript' src='http://partner.googleadservices.com/gampad/google_service.js'></script>
<script type='text/javascript'>
GS_googleAddAdSenseService("ca-pub-2838961657718357");
GS_googleEnableAllServices();
</script>
<script type='text/javascript'>
GA_googleAddSlot("ca-pub-2838961657718357", "Top728x90");
GA_googleAddSlot("ca-pub-2838961657718357", "Right300x250");
GA_googleAddSlot("ca-pub-2838961657718357", "Right300x250Bottom");
</script>
<script type='text/javascript'>
GA_googleFetchAds();
</script>
</head>
<body>
<!-- R300x250_L -->
<div id='div-gpt-ad-1367703773182-0' style='width:300px; height:250px;'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1367703773182-0'); });
</script>
</div>
<!-- Right300x250Bottom -->
<script type='text/javascript'>
GA_googleFillSlot("Right300x250Bottom");
</script>
</body>
</html>
I was trying to include the Google Plus One button in a Meteor app for collaborative sketching and I noticed that the script tags inside templates are not executed.
<template name="gplus">
<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-href="{{url}}"></div>
<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
console.log("Google Plus button");
(function() {
var po = document.createElement('script');
po.type = 'text/javascript';
po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
</script>
</template>
I got the button to work by moving the script to a separate JavaScript file, but I still wonder why the inline-script didn't work.
I see the script tag on the console, but the script itself doesn't run. How does Meteor do this?
Meteor just inserts the contents of the gplus template into the DOM, so of course nothing happens because there is no script execution when elements are added to the DOM.
To fix this you can create a .js file and put it in the client folder, which Meteor will automatically include and execute for you (as well as minify in production).
Here is what I do to load the Google +1 Button in my application:
1. Add the JS library between the <head> tag of your application.
<head>
<!-- Load JS Library -->
<script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script>
</head>
2. Insert the +1 Button.
<template name="myTemplate">
<div data-href="https://hackisition.com/" data-size="medium" class="g-plusone"></div>
</template>
3. Render the Button.
Template.myTemplate.rendered = function() {
return gapi.plusone.go();
};
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_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>
Google analytics provided me this script to put up on my website to track the visitors of my website, but i have observed that my own visits are also counted into the analysis. How do I disable this script if the admin is logged in to the wordpress account as the current user?
If any user is logged in.
<?php if ( !is_user_logged_in()) { ?>
<script type="text/javascript">
GA code
</script>
<?php } ?>
http://codex.wordpress.org/Function_Reference/is_user_logged_in
To select a particular user, you need to use if($user_id) {
This is one of the alternatives I can think of. Using this code the GA code won't execute for any Author, Contributor nor Admin, but In case your site allows Subscribers to log in you will track em.
<?php if ( !current_user_can('edit_posts') ) { ?>
GA code
<?php } ?>