Google Analytics User ID view empty - google-analytics

I have 2 websites running on 2 separate servers.
The "public" one is www.projacked.com
The "members only" one is global.projacked.com
I enabled cross domain tracking using the linker plugin.
In the internal page I have the following (it's twig on Symfony):
ga('create', 'UA-XXXXXXXX-X', 'auto', {allowLinker: true});
ga('send', 'pageview');
{% if app.user is not null %}
ga('set', 'userId', '{{ app.user.id ~ '-' ~ app.user.username }}');
{% endif %}
// Loads the Linker plugin
ga('require', 'linker');
// Instructs the Linker plugin to automatically add linker parameters
// to all links and forms pointing to the domain "projacked.com".
ga('linker:autoLink', ['projacked.com'], false, true);
As an example of how the line that sets the user id gets rendered by the template we have:
ga('set', 'userId', '1201056-justintrudeau');
This line does not report any error in javascript, and I tested that it gets called.
I created a separate view for this. I tried as website to track either www.projacked.com or global.projacked.com. I've enabled user id for this view. In any case I cannot see any user on this view.
This is the configuration for the view:
I've checked that the snipped is with the same UA-XXXXXXXX-X (of course my code).
Questions:
shall I use the members-only website in the view, as it's the only one that can set the userID?
do I have to make any change in property? There I have only one website (www.projacked.com). Do I have to do anything to make it register stuff also related to the other website (global.projacked.com), apart from adding the same UA-XXXXXXXX-X there too?
why on hell I don't see the users on the members-only site?
Thank you

From your code it looks like you are setting the userId after your pageview call, so it is never actually sent to the server. Move it upwards and things should start to work ("set" calls are not hoisted, they are applied only to hits that follow the "set").
Also the user id and name might be personally identifiable information and would need to be hashed with at least SHA256 before your are allowed to store it within Google Analytics.

Related

Google Global Site Tag: how to override page referrer

I set up Google Global Site Tag for an Angular SPA. I'd like to override the page referrer that is passed to Google.
Use case: after logging in (using oAuth2), the user is redirected to the app. At this point, the url contains sensitive auth data (auth code). This url then gets logged to Google Analytics, which of course should be avoided.
I use the Angulartics2 lib with the GST provider, which internally uses the following code to send page track events to Google (according to Google's documentation on SPA route tracking):
gtag('config', trackingId, params);
By including the following line, I'm able to override the referrer (dr) in the query parameters:
params.page_referrer = 'https://my-custom-referrer.com';
index.html:
<script async src="https://www.googletagmanager.com/gtag/js"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
</script>
Code for tracking the page view event:
gtag('config', 'G-XXXXXXXXXX', {
page_location: 'http://localhost:4200',
page_path: '',
page_referrer: 'https://my-custom-referrer.com',
});
This leads to the following request being sent to google (note the dr properties, which hold the referrer):
As you can see:
The query string uses the correct referrer
However, the request payload still contains the original referrer containing sensitive data
I've tried setting the page referrer with the following line of code:
gtag('set', { page_referrer: 'https://my-custom-referrer.com' });
but this results in the same behaviour.
How can I make sure the request body also uses the overridden referrer? There used to be a setting within the Analytics dashboard to exclude certain referrers, but in the new version (Google Analytics for properties) it has disappeared.
Have you checked out the referral exclusion list?
Generally, if it redirects off your domain for authentication, you don't really want to count it as a referral, because then it will replace the true acquisition data for your users. You want to exclude it so the session is kept intact.
Also, you stated that you're using google tag manager (GTM), but your code and links to the documentation are for gtag.js, which is NOT GTM.
To set the referrer field in GTM, you need to locate the GA tag OR GA Settings tag and under the "fields to set" area, set the referrer field to your liking. Like so:
Though careful that if you override this for everything there could be issues with data accuracy, so you want to conditionally override this based on where it is coming from.
Not a solution, but a workaround. It pushes the current page (without paths or query parameters) to the history, which is then used by Google as the page referrer. At least it allows you to keep sensitive information out of Google Analytics.
history.pushState({ page: 1 }, '', window.location.origin);

How to add a separate Analytics for each website page in Wordpress

We have a WordPress installation that has locations as separate pages.
For example: mysite.com/colorado and mysite.com/alabama
We need separate google analytics for each of these as well as 1 for all of mysite.com.
Is there a way to do this with a WordPress plugin(s) or will we need to hand code some things?
Thanks in advance!
If I'm not mistaken, Google Analytics lets you view your analytics in a breakdown like that. That said, if you do need individual scripts, it would be relatively easy to program in. There may be some plugins that do this, but I'm not aware of any in particular, though a cursory glance showed plugins like Header and Footer Scripts that allow you to add scripts on a page by page basis.
Some themes also allow you to add SEO/Script settings per page/post. If that's the case, you can just open up each page and dump each script tag in the "header scripts" or similar section, and call it good. (Genesis is an example of a theme that does this).
If not, programming this would be relatively straight forward. I'd do it something like this:
add_action( 'wp_head', 'display_analytics_by_page', 1 );
function display_analytics_by_page(){
// Default Script Code with individual UA codes replaced
$script = '<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=[UA-CODE]"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());
gtag(\'config\', \'[UA-CODE]\');
</script>';
// Array of UA codes by state
$codes = array(
'Alabama' => 'UA-123456789-1',
'Oregon' => 'UA-987654542-3',
'Vermont' => 'UA-000000000-0'
);
// Get title of this page
$title = get_the_title();
// If this page title exists in the codes array, swap placeholder and echo it.
if( isset($codes[$title]) ){
echo str_replace( '[UA-CODE]', $codes[$title], $script );
}
}
I commented along the way, but the gist is to put in the "default" script, but pull out the UA code. This will only work if you need the same exact script code in each one, otherwise you'll have to add each script to the $codes array instead.
Then create an array of the UA Codes (or full scripts if needed), keyed by the page title.
Then check the page title, and if that exists, pull that code in and echo it. This is run on the wp_head hook, so you just need to put this code in your functions.php (or similar) file.
If you want to go easy with no-coding, then go with a plugin. Jump to Plugins > Add New and Search for Google Analytics in the search box. Install the plugin named Google Analytics for WordPress (Formerly GADWP). Activate and connect the plugin with your Google Analytics property. When all is done, you will see a new tab beside your post's title. See a screenshot here.
And if you want the net analytics for the whole web site then head to your admin dashboard. A new widget will appear there with the analytics.
Documentation for the plugin can be found here.
I hope it helps.
I'm curious why you need a separate GA account for all the locations? Common practice in this scenario would be:
Use 1 account
Create 1 GA view for the entire domain
Create +1 view for each location by filtering traffic based on the URL
If the different accounts are related to limiting user access, know that you can grant user access based on each property view.

How can I set Analytics variables with Google Tag Manager from the server?

I'm using Google Optimize for creating A/B tests. I'm using it in server side mode as in this guide: https://developers.google.com/optimize/devguides/experiments
That guide shows an easy way of setting which experiment is running with which variant by rendering the JS code on the server which sets the experiment id and variant id:
// 2. Create a tracker.
ga('create', 'UA-XXXXX-Y', 'auto');
<?php
<<<HTML
// 3. Set the experiment ID and variation ID.
ga('set', 'exp', '$experimentId.$variationId');
HTML;
?>
// 4. Send a pageview hit to Google Analytics.
ga('send', 'pageview');
However I'm using Google Tag manager and so far haven't managed to find any guide that shows how to set variables from the server with it. ga is an undefined variable so the above doesn't work.
Since the GTM calls the normal snippet for each tag, you can set any field the analytics snippet understands even if they are not yet automatically listed in the tag editor drop down.
For example, as a page view field:
Then set DataLayer variable so it can be received from the external source, for example:
Leading to the fields using the variable in a finished Tag:
Now, you can set DataLayer variable in the server side that will be passed through to the tag. Since I chose a page view, it would be best to pre-fill the dataLayer before loading the GTM so they are present before the initial tags fire, for example:
<!-- Google Tag Manager -->
<?php or other backend language wrapping...
<script>window.dataLayer = [{exp:"$experimentId.$experimentVariant"}]
</script>
?>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-0');</script>
<!-- End Google Tag Manager -->

GTM/UA Track multiple authors

Seems like this would be quite simple but i cannot find much documentation available. Currently on our article system, each author name appears like this on each page:
<div id="author-info">
Mr. Man
According to Google, using custom dimensions is the way to do track authors in UA. Setup a custom dimension "Author" and set the scope to Hit/Session/User. Not sure i fully understand the different scope options. I get the following code:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);
Which i'm assuming should be:
var author-id = 'Mr. Man';
ga('set', 'dimension1', author-id);
Is that correct?
Also, how can i pass the author information into the dataLayer and then call the author page views through GTM? How about multiple authors?
Ok, this answer comes from the G+ analytics community (link below). Credit goes to https://plus.google.com/u/0/+SimoAhava
Using GTM, we can do the whole thing using macros and set fields:
Create a Custom JavaScript Macro which returns the author name:
function() {
try{
return document.getElementById("author-info").children[0].text;
} catch(e) {
return "";
}
}
In your Pageview tag, go to More Settings -> Custom Dimensions, add custom dimension, put the index number of the CD you created in the required field and add the JavaScript macro to the Dimension field.
This setup grabs the text content of the page's author link and sends it as a custom dimension with the pageview. Since it's hit-scope, every pageview will be attached with the author info of the page.
https://plus.google.com/104258622890980927916/posts/e6cC6y8q1m6

Wordpress Sign-ups Using GA Goal Tracking

I'm trying to use GA Tracking to Track Wordpress Sign-ups on my site. I'm assuming I need to use Goal Tracking and NOT Event tracking for this. I tried setting up goal tracking but it's not working. I am not using any GA plugins like Yoast, etc.
I can't get this to work with Wordpress:
I set my Goal URL to: /wp-login.php?checkemail=registered (URL landing page for sign-ups)
Match Type: "Exact Match"
No Goal Funnel was set.
GA is not tracking any of my sign-ups. What I am doing wrong here? My site is www.StreetofWalls.com if you would like to test.
The script that loads google-analytics isn't on the login page, so you're not getting any of the /wp-login.php?checkemail=registered pageviews sent back to google.
Put this on your wp-login.php page
<script type='text/javascript' src='http://www.streetofwalls.com/wp-content/themes/sow3/js/google-analytics.js'></script>
While TomFuertes's answer will work, it is not good to modify core WordPress files like wp-login.php. Updates to WordPress will overwrite any changes that you make and avoiding WordPress updates is a very bad idea.
I recommend adding the code using the login_head hook in your functions.php file:
function add_ga_to_login_page(){
echo '<script>';
echo 'GA TRACKING CODE HERE';
echo '</script>';
}
add_action('login_head', 'add_ga_to_login_page');

Resources