Google Analytics: Test event tracking with test profile - google-analytics

With Universal analytics, what are the best practices in sending/testing data to a test profile?
I've included the tracking snippet before the </head> and the following:
window['ga-disable-UA-XXXXXXXX-X'] = true;
ga('create', 'UA-XXXXXXXX-X', {
'cookieDomain': 'none'
});
ga('send', 'pageview');

I'll usually handle this by always keeping the same code in production vs staging (aka remove the ga-disable and cookieDomain lines of your code snippet). I then create 2-5 different views in Google Analytics and filter them as:
Default (include only production domains - eg: regex ^(www\.)?example\.com$)
Raw Data (no filters)
Test Site (inlude only test domains - eg: exact test.example.com)
Staging Site (include only test domains - eg: exact staging.example.com)
Admin Site (include only admin domain - eg: exact admin.example.com)
Under Profile >> Filters there are predefined filters and custom filters. Use a predefined include Traffic to the Domain for all of the above.

Related

Where does gtag pageview's 'page_location' get reported?

I'm using the Google Analytics's gtag.js on a single-page webapp (SPA). As suggested in the docs for SPAs, I've disabled the automatic send_page_view and instead explicitly send a page view by setting a page_path and page_title to the gtag config. So far, so good.
The docs also list an optional page_location parameter to send the page's full URL. For my app's search page, I'm particularly interested in getting the full query params after the ? in the URL, so I tried adding it to that payload:
gtag('config', GA_TRACKING_ID, {
page_location: window.location.href,
page_title: 'app search page',
page_path: '/search',
});
However I never see a full URL in either the Anlaytics web UI or in its export to BigQuery. I only the page_title and page_path. Where does page_location end up getting stored or reported?
page_location is not reported in Analytics but might be used for processing source and medium parameters and for extracting page_path if not provided.

How to configure gatsby-plugin-google-analytics with cookies consent?

I have developed a website using gatsby and I am using google analytics plugin via gatsby-plugin-google-analytics, now to be nice with the users, I would like to add a cookie consent where the user will be two options whether to accept or decline cookies usages. If the user declines then I would like to stop google analytics to track the user activity. I dug into their documentation but unable to find the option to achieve this, is there any way around to achieve this.
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: siteConfig.googleAnalyticsId,
// Defines where to place the tracking script - `true` in the head and `false` in the body
head: false,
},
},
For the movement my gatsby-config.js looks like this. how one can achieve this.
Thanks you in advance.
The plugin merely loads the library and instruments page tracking calls in a Gatsby-compatible way for you. All of the other Google Analytics calls, including disabling measurement for a user works the same as normal.
It's up to you to:
Build and show a cookie notice with an opt-out
Remember when a user has opted out
Communicate this on each page-load for the user (before any ga() calls are made) by setting window['ga-disable-UA-XXXXX-Y'] = true
You should be able to do this in a function exported as onClientEntry from gatsby-browser.js. For example:
export const onClientEntry = () => {
if (userHasOptedOutOfThirdPartyTracking()) {
window[`ga-disable-${process.env.GATSBY_GOOGLE_ANALYTICS_ID`] = true
}
}
For the Gatsby website, you could use a combination of gatsby-plugin-gdpr-cookies for analytics cookies management in your gatsby-config.js and react-cookie-consent to display the cookie banner.
# using npm
npm install --save gatsby-plugin-gdpr-cookies react-cookie-consent
# using yarn
yarn add gatsby-plugin-gdpr-cookies react-cookie-consent
You can find a more detailed tutorial here.

Google workbox offline analytics not working as expected

I am trying to follow the example in:
https://developers.google.com/web/tools/workbox/modules/workbox-google-analytics
I set up a Google analytics account, and then created a PWA using https://codelabs.developers.google.com/codelabs/workbox-lab/#0.
The PWA from the lab works as expected.
Then I created a custom dimension (dimension1) and a custom metric (metric1) in google analytics. When I send the dimension and metric in code on page load it shows up in the Google analytics reports.
ga('create', 'UA-125355230-1', 'auto');
//ga('send', 'event', 'category', 'action', { 'metric1': 100 });
ga('set', 'dimension1', 'online');
ga('send', 'pageview');
Then in the sw.js (the service worker js), I added the following code, as it says in the tutorial:
workbox.googleAnalytics.initialize({
parameterOverrides: {
dimension1: 'offline',
},
hitFilter: (params) => {
const queueTimeInSeconds = Math.round(params.get('qt') / 1000);
params.set('metric1', queueTimeInSeconds);
},
});
I stopped the app, and refreshed the index.html a few times. I brought the app back online and tried to sync the service worker by using the queue name workbox-background-sync:workbox-google-analytics.
This triggers the background plugin code, but it does not find anything to send.
Same results when I tried to make the network offline in devtools.
In debugger the createRequestWillReplayCallback of workbox-google-analytics.dev.js is called initially, but the replayRequests of workbox-background-sync.dev.js which is called when I try syncing seems to not find anything to replay.
During sync the
workbox-background-sync - http://localhost:8081
is created in indexedDB, but the queueName under requests seem to be empty:
What am I missing? Any pointers or help to get the offline analytics working with Workbox and test PWA is appreciated.

Verify correct Google Analytics setup by inspecting the tracker object in a single page app

I have a single page app that uses Google Analytics. The account key and domain change per-environment because the tracking is meant to be segmented by environment so the setup is somewhat involved.
I would like to be able to verify that the _gaq object has my account key as expected and that is has the domain name set as expected. When I run the code google provides to get the account key, I only get back A-XXXXX-X.
_gaq.push(function() {
var pageTracker = _gat._getTrackerByName();
console.log(pageTracker._getAccount()) // Logs UA-XXXXX-X, not my actual number
});
What is the right way to verify the correct configuration of the _gaq object?
You can verify correct configuration by running these commands in console:
ga(function(tracker) {
console.log( tracker.get('trackingId') )
console.log( tracker.get('cookieDomain') )
});
This will log out the tracking ID and domain google is expecting you to track against. If the configuration is set up correctly, these values will be what you have configured.

Google Analytics and the __utm.gif file

When using Google Analytics and using the newer ga.js file, is the __utm.gif file needed in your root directory of your web site?
I do see a call being made from my browswer to the file on google-analytics.com (http://www.google-analytics.com/__utm.gif?...), but in the past I was told that it needed to exist in the root of the site.
Does this still hold true?
Could you please provide a reference?
If you are using the new version of the tracking code (ga.js), the only thing you need to do is include this snippet of JavaScript:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
</script>
This would imply that there are no requirements for you to host any __utm.gif file on your server (I'm not sure how this would even work for analytics anyway, as the whole point is for the client's browsers to make requests to Google's servers).
I use the older urchin.js tracking mechansim and there are no requirements to host anything locally although you can put the .js file on your server for speed. There are four images that get requested to provide the tracking; __utma, __utmb, __utmc and __utmz which correspond to they cookies recorded of the same name.
From Google; When you first begin implementing tracking in Google Analytics website, you need to install the tracking code on your website pages. The generic tracking code snippet consists of two parts: a script tag that references the ga.js tracking code, and another script that executes the tracking code.
You are required to add _utm.gif to your server root if you are also sending a copy of the google analytics data to your local webserver. It gives you the option of keep the data much longer since Google is only keeping 25 months of data for free account.
Google also doesn't share the raw data, the only way to get the raw data is also sending it to your local webserver log. For it to work, you need to add this line below to the tracking code:
_gaq.push(['_setLocalRemoteServerMode']);
For more detailed discussion, please refer to the book by Clifton, Brian (2012-03-30). Advanced Web Metrics with Google Analytics (Kindle Locations 4459-4460). John Wiley and Sons.

Resources