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 %}!
Related
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#">
We have an internal WordPress site, and about 25 users. Our current Google analytics set-up will show us how many times a page has been visited, but because everyone comes from the same IP address it thinks that it is basically one very industrious person clicking a lot.
Does anyone have a strategy for tracking individual users?
(They are all logged into WordPress as a function of our single sign on.)
You can use the _setCustomVar method from the JavaScript API to provide the user name of the current user. To my knowledge no GA plugins for Wordpress support this, so you will need to put your tracking code directly into the theme or write a custom plugin for it. The custom variable will then show up as a segment in Google Analytics. To get the current user you can use the wp_get_current_user API call.
Your tracking code would then look something like this:
<?php
if (is_user_logged_in()) {
$user = wp_get_current_user();
$userName = $user->user_login;
}
?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
<?php if (isset($userName)) : ?>
_gaq.push(['_setCustomVar', 1, 'Username', <?php echo(json_encode($userName)); ?>, 1]);
<?php endif; ?>
_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>
For the Universal Analytics version:
<?php
if (is_user_logged_in()) {
$user = wp_get_current_user();
$userName = $user->user_login;
}
?>
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXX-1', 'auto');
<?php if (isset($userName)) : ?>
ga('set', 'userId', <?php echo(json_encode($userName)); ?>); // Set the user ID using signed-in user_id.
<?php endif; ?>
ga('send', 'pageview');
</script>
I co-authored called Stream that tracks logged-in user activity. Basically, it's designed to be a detailed audit trail of everything that happens in the WP Admin area.
It also organizes the activity by user, context, action and IP address so it can be easily filtered/searched later.
More information:
http://wptavern.com/stream-a-wordpress-plugin-to-track-and-monitor-changes-in-the-admin
http://x-team.com/2013/12/stream-track-every-change-made-on-your-wordpress-site/
https://github.com/x-team/wp-stream
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>
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.
<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 } ?>