Tracking internal users on wordpress - wordpress

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

Related

basic GA4 purchase tag not tracking

I'm on GA4 and this basic tag is not showing in my google analytics
gtag("event", "purchase", '.\yii\helpers\Json::htmlEncode([
"transaction_id" => $payment_id,
"value" => $value,
"currency" => $currency,
]).');
here is my gtag
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXX');
</script>
Any idea why? the values are all there when i echo them, but it's still not tracking.

Best place to add analytics in ApostropheCMS

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 %}!

How to handle multiple GA, AdWords, Remarketing tags

I'm looking for some info on how to best manage multiple Google AdWords and other tags. My primary question is how to handle both the regular AdWord tag, the standard AdWords Remarketing tags and one of the custom AdWords Remarketing tags I created - say a 180 day RLSA list tag. Do I need a separate tag call (vars, call to the conversion script and the noscript img tag)? Is there a way to combine?
Also - being an ecommerce site - I see that for AdWords I need to add my sales total to the noscript img tag. But what about to the remarketing one? It has a default value of 1.00, instructions make no mention to change that and the js version doesn't have any fields for it so I assume 1.00 is correct?
I am adding my entire code for the receipt of my store which has GA, standard AdWord and as well as the 180 day RLSA tag (which I assume is valid as the basic Remarketing tag). Docs don't seem to ever address multiple tags unfortunately.
<!-- BEGIN: AdWords -->
<script type="text/javascript">
var google_conversion_id = 1002886276;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "DZAUCPS-jQoQhKmb3gM";
var google_conversion_value = 64.98;
var google_conversion_currency = "USD";
var google_remarketing_only = false;
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/1002886276/?value=64.98&currency_code=USD&label=DZAUCPS-jQoQhKmb3gM&guid=ON&script=0" />
</div>
</noscript>
<!-- END: AdWords -->
<!-- BEGIN: Analytics -->
<script type="text/javascript">
(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-40148609-3', {
userId: '750fd151-6852-4a35-974d-c99f85f993f3'
});
ga('send', 'pageview', {
'title': 'Receipt'
});
ga('send', 'event', 'Completed Order', {
category: 'Shopping',
revenue: 64.98,
id: 734352106,
total: 64.98,
shipping: 0,
tax: 0,
products: []
});
ga('set', 'ecomm_pagetype': 'purchase');
ga('set', 'ecomm_totalvalue', 64.98);
ga('set', 'ecomm_prodid': [218825388699, 118825388699])
</script>
<!-- END: Analytics -->
<!-- BEGIN: AdWords Remarketing -->
<script type="text/javascript">
var google_conversion_id = 1002886276;
var google_conversion_label = "9NX_CKTrkgoQhKmb3gM";
var google_custom_params = {
ecomm_pagetype: 'purchase',
ecomm_totalvalue: 64.98,
ecomm_prodid: [218825388699, 118825388699]
};
var google_remarketing_only = true;
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/1002886276/?value=1.00&currency_code=USD&label=9NX_CKTrkgoQhKmb3gM&guid=ON&script=0" />
</div>
</noscript>
<!-- END: AdWords Remarketing-->
Thanks for any assistance or official guides to multiple tag management.
The question is old, yet I came here from Google just before I found an answer to a similar problem, so I'll try to share my conclusions. It is best to integrate Google Tag Manager in case of having multiple codes. I consider it to be the best practice for your example.

How to disable javascript not executing in iphone?

I don't want to execute the script if the device is an iPad or iPhone. But it's showing error in the console. What is the reason for this?
<script>
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (!agentID) {
document.write('<script src="<?php echo get_template_directory_uri(). "/js/modernizr-2.6.2.min.js"?>"></script>'";
}
</script>
My current code is:
<script> var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (!agentID)
document.write('<script src="<?php echo get_template_directory_uri(). "/js/modernizr-2.6.2.min.js"?>"></script>'
</script>
You miss some characters in your 4th line, try to edit this line.
You can also get the url in a specific var before your script in PHP, it'll be easier to avoid errors
$url = get_template_directory_uri() . "/js/modernizr-2.6.2.min.js";
...
<script> var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (!agentID)
document.write('<script src="<?php echo $url ?>"></script>');
</script>

How do disable Google Analytics JS code in wordpress when the admin is logged in

<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 } ?>

Resources