I am trying to set up google analytics to track an event, the event will basically be fired from a back end which will load up an HTML page that sends the event over to google. I have the following code in my html page:
<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-XXXXXXXX-1', 'auto');
ga('send', 'event', 'Telephone', 'Answered', '123456789', 112);
</script>
12345
I have the google analytics debugger installed, and I have followed the documentation at https://developers.google.com/analytics/devguides/collection/analyticsjs/events I have even done a simple href link using the exact example, but it just doesn't work, all get in the debugger is:
Running command: ga("send", "event", "Telephone", "Answered", "123456789", 112)
Unallowed document protocol. Aborting hit.
Registered new plugin: ga(provide, "render", Function)
Running command: ga("send", "event", "Videos", "play", "Fall Campaign")
Unallowed document protocol. Aborting hit.
I am running this locally just to test it out, if that makes a difference?
I've also been checking Analytics real time events and nothing has been registered.
Any help would be gratefully received.
Thanks
David
I've sorted it, it works fine when it's loaded on the server, but not locally!
Cheers
Related
I wanted to use the new analytics consent mode which is gdpr compliant following this official guide
Placed following into the header:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {'ad_storage': 'denied', 'analytics_storage': 'denied'});
</script>
<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-XXXXXXXX');
</script>
Analytics consent mode works with:
gtag('consent', 'default', {'ad_storage': 'denied'})
However no longer works (no user activity on dashboard) if I deny cookies in the settings like so:
gtag('consent', 'default', {'ad_storage': 'denied', 'analytics_storage': 'denied'});
My understanding is that it should still reflect page hits without the cookie.
I can see the POST request being sent to their go server with status 204.
What am I doing wrong?
The hits with 'analytics_storage' set to 'denied' are not shown in GA reports at all now.
Initially I could see them in the real-time report, but now that seems to be gone too.
Having said that, it is possible to manipulate the request with the customTask API and rewrite the 'gcs' parameter (or drop it altogether). This way, you would see the anonymized (on user level) hits in GA while not using cookies.
Currently (beta) with analytics_storage denied Google Analytics will not use the _ga cookie to identify the user, even if already present in the browser, but will associate a temporary identifier that only exists on the current page. This guarantees the non-traceability of navigation however, despite the interactions being sent to the Google Analytics servers, the data set in question, characterized by a parameter that identifies the consent method, is not currently collected / exposed in the reports.
Anyway GA4 always sets IP anonymization by default, so this thing doesn't need to be managed.
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.
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.
I have sent an exception on the client:
gtag('event', 'exception', {
description,
});
But looks like only non exception events are visible in real-time report.
My question:
Is there a way to enable it?
Can I build a custom real-time report?
Or do I have to work around it with a different event category? EG:
gtag('event', 'error', {
description,
});
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.