Google Analytics Opt-out - google-analytics

To be OK with the nonsense cookie law of the Grosse European Komission, I've built this little script to opt-out the GA tracking code. The result is a popup asking once in the session for OK vs CANCEL GA. What do you think of it?
(function() {
var message = "GOOGLE ANALYTICS COOKIE\nCookie or not cookie, that is the question\n\nI opt for Google Analytics pageview anonymous tracking.\n\nJe consens au suivi d'audience anonyme Google Analytics.";
if (!sessionStorage['key_name']) {
if (confirm(message) == true) {
sessionStorage.setItem('key_name', 'gaOK');
}
else {
sessionStorage.setItem('key_name', 'gaOut');
}
}
if (sessionStorage['key_name'] == 'gaOK') {
(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-N', 'auto');
ga('send', 'pageview');
}
})();

Related

google-analytics: Conversions not showing on Transactions

I have installed Google tag manager and the following tag is firing and being count accordingly for my E-commerce conversion (tag called 'Google Analytics eCommerce'):
<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-76425812-2', 'auto');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
'id': inspectionID
});
ga('ecommerce:addItem', {
'id': inspectionID,
'name': 'Inspection',
'quantity': 1
});
ga('ecommerce:send');
</script>
Nevertheless I am trying to create a tag and to avoid firing multiple tags for the same conversion. For that, I am creating a universal tag to save the utm_source and I am storing it on a Custom Javascript Variable.
TAG:
<script type="text/javascript">
var url = window.location.href;
var keyToFind = 'utm_source';
if(url.indexOf(keyToFind + '=') > -1)
{
var valueFound = url.substr(url.indexOf(keyToFind + '=') + keyToFind.length + 1).split('&')[0];
sessionStorage.setItem(keyToFind, valueFound);
}
</script>
Java Script Variable:
function()
{
return sessionStorage.getItem('utm_source');
}
When I use this variable to trigger any other tag (this is not triggering the Google Analytics eCommerce tag at all), GA stops considering the conversions.
Anyone may have an idea about what can be happening in the case?
Thanks,
Helton

Google analytics not tracking enhanced ecommerce Data

i manage several hotels on analytics, actually have like 400, and we are updating the ecommerce data to enhanced ecommerce.
when i updated the scripts to e ecommerce on 100 hotels, no one was working, so i ran a test and i found that the isogram analytics code wasnt implemented, fixed that and now 70/100 hotels are getting ecommerce data.
idk if is matter of time to get the data on the last 30 hotels but here is the code, if anyone can see anything wrong, thanks.
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'viewcode', 'auto', {'allowLinker': true});
// Load the plugin.
ga('require', 'linker');
// Define which domains to autoLink.
ga('linker:autoLink',['websites']);
ga('send', 'pageview');
ga('require', 'ec');
ga('set', '&cu', 'MXN');
ga('ec:addProduct', {
'id': '{SkuCode}',
'name': '{ProductName}',
'category': '{Category}',
'price': '{UnitPrice}',
'quantity': {Quantity}
});
ga('ec:setAction', 'purchase', {
id: '{NoReservation}',
revenue: '{Total}',
tax: '{Tax}'
});
ga('send', 'event');
</script>
The analytics code that you are using it looks right, however in order to assure that analytics code is pushed from the all the hotels websites I recommend you to follow the following steps:
1) Check that hotels domains name are correctly in the "linker" parameter
2) Verify that those websites dont't have another GA analytics code that overwrite your code. In order to avoid conflicts I recommend you to rename the "ga" global object to "myga" or something like that. Example:
<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','//www.google-analytics.com/analytics.js','myga');
myga('create', 'viewcode', 'auto', {'allowLinker': true});
// Load the plugin.
myga('require', 'linker');
// Define which domains to autoLink.
myga('linker:autoLink',['websites']);
myga('send', 'pageview');
...
You can check when a hotel is pushing events if you are using the GA Real-time in the GA dashboard.

Polymer - Track app usage with Google Analytics

I'm trying to use Google Analytics (web) with my polymer app (updating the tracker object in routing.html as per this GA SPA doc). I used the Polymer Starter Kit as a starting point. I'm not seeing any pageviews though, apart from / - what's the suggested way of tracking app usage?
routing.html
page('/topstories', function () {
app.route = 'topstories';
window.ga('set', 'page', '/topstories');
});
page('/about', function () {
app.route = 'about';
window.ga('set', 'page', '/about');
});
index.html
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxxx-1', 'auto');
ga('send', 'pageview');
</script>
In addition to setting the page value on the tracker, you also have to send the pageview hit to Google Analytics. In your route callback functions you'll need to add the line:
ga('send', 'pageview');
You could also write a function that does all this for you, so you don't have to repeat the set and send calls every time.
function updatePage(path) {
return function() {
app.route = path.slice(1);
ga('set', 'page', path);
ga('send', 'pageview');
}
}
The your page route declarations would look like this:
page('/topstories', updatePage('/topstories'));
page('/about', updatePage('/about'));
My solution takes advantage of the middleware-ish page.js handler:
// Routes
page('*', scrollToTop, closeDrawer, function(ctx, next) {
ga('set', 'page', ctx.path); // simply add
ga('send', 'pageview'); // these rows
next();
});
But this also fires as the / route is initialised, so to not double count visits, remove the final ga('send', 'pageview'); from your <script> block.

Tracking anchor links - Google Analytics

I have a wordpress website and I'm tracking it with google analytics. I need to track the anchors and
have been digging stackoverflow to get some answers. After trying several, I still can't get it to work. So, what's wrong with this?
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-47361493-1', 'festasdelisboa.com', {'allowAnchor': true});
ga('require', 'linkid', 'linkid.js');
ga('send', 'pageview');
$(function(){
$("a[href*='http']").each(function() {
$(this).click(function (ev) {
var pageView = '/outgoing/' + $(this).attr('href');
_gat._getTrackerByName()._trackEvent('Outbound Links', pageView);
var _href = $(this).attr('href');
setTimeout(function() {
location.href = _href;
}, 100);
ev.preventDefault();
return false;
});
});
$("a[href*='mailto']").each(function() {
$(this).click(function (ev) {
var pageView = '/mailto/' + $(this).attr('href').substring(7);
_gat._getTrackerByName()._trackEvent('Mailto', pageView);
});
});
$("a[href*='#']").each(function() {
$(this).click(function (ev) {
var pageView = '/anchor/' + $(this).attr('href').substring(1);
_gat._getTrackerByName()._trackEvent('Anchors', pageView);
});
});
});
</script>
Was struggling with this for quite a while now. Use the below snippet as part of your Universal Analytics code - found it worked almost immediately for me! My GA reports are showing visits on hashtag anchors in the Behaviour >> Site Content >> Content Drilldown section
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-12345678-1', 'auto');
ga('send', 'pageview');
// Anchor Tracking snippet start
jQuery(document).ready(function () { var
hashtag = /#\S+/ jQuery('.menu li a').click(function(){ var match =
jQuery(this).attr('href').match(hashtag); ga('send', 'pageview', '/' +
match[0]); }) });
// Anchor Tracking snippet end
</script>
Found this to be the only workable and easily understandable solution - when it comes to Google's Universal Analytics

Multi google codes in one page

We have this code on the website.
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-40584814-1', 'webistename.com');
ga('send', 'pageview');
</script>
How can I add another account to it? Can I add something like this?
Can I add something like this?
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-40584814-1', 'webistename.com');
**ga('create', 'UA-472147-1', 'webistename.com');**
ga('send', 'pageview');
</script>
I tried that and I can't see the stats on the new account. How can I achieve this? I know I could invite someone via an email but I want to add a completely new account.
Thanks!
Since you're using Universal Analytics (not the original Async version), you have to pass it within the create statement via the options object:
ga('create', 'UA-XXXXXX-X');
ga('create', 'UA-XXXXXX-Y', 'auto', {
'name': 'foo'
});
// later on...
ga('send', 'pageview'); // sends to account XXXXXX-X
ga('foo.send', 'pageview'); // sends to account XXXXXX-Y
The documentation
Asynchronous tracking code my friend ;)
https://developers.google.com/analytics/devguides/collection/gajs/?hl=pl#MultipleCommands

Resources