Google analytics, how _setDomainName works with iframes? - iframe

on our site we would like to track with Google Analytics user actions in iframe content. The iframes are hosted on a different and 3rd level domain. For some reason my setup doesn't work.
Situation:
In the iframe I want to use two google trackers: one from the parent site, and a separate just for the iframe. I also want to use Events and VPVs. When browsed the page can be seen in GA account's "real-time overview" section. But I don't see a VPV which is attached to Submit button click.
I also don't really understand how function "_setDomainName" works and what should be there. These are separate issues and any help would be much appreciated.
The setup:
On parent site:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', 'parent.com']);
_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);
})();
$(document).ready(function() {
var pageTracker = _gat._getTrackerByName();
var iframe = document.getElementById('myIFrame');
iframe.src = pageTracker._getLinkerUrl('http://mydomain.iframedomain.net/page.aspx');
});
</script>
ON Iframe page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['a._setAccount', 'UA-XXXXXX-1']);
_gaq.push(['a._setDomainName', 'parent.com']);
_gaq.push(['a._setAllowLinker', true]);
_gaq.push(['a._trackPageview']);
/* this is second tracking account */
_gaq.push(['b._setAccount', 'UA-XXXXXX-2']);
_gaq.push(['b._setDomainName', 'iframedomain.net']);
_gaq.push(['b._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>
Then on a button click event I call this function:
function track() {
_gaq.push(['_trackPageview', '/search-retailer-clicked']);
_gaq.push(['_trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
}
what am I doing wrong?

You've named both trackers in the iframe (a._setAccount & b._setAccount), but in the button click event, you're using a default, unnamed tracker.
The outcome is that the virtual page view and event are getting sent to a tracker that hasn't had the account set. If you look at the tracking pixel requests (via ga_debug.js, firebug, chrome developer tools, or fiddler), they'll show a UA-XXXXX-X accountId.
You probably want something more like:
function track() {
_gaq.push(['a._trackPageview', '/search-retailer-clicked']);
_gaq.push(['a._trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
_gaq.push(['b._trackPageview', '/search-retailer-clicked']);
_gaq.push(['b._trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
}

Related

Google Analytics - E-Commerce portal - Track order flow

I'm working on an E-commerce portal and tracking my portal using Google Analytics, could see stats like,
Page Views
Unique Page Views
Location etc
Products could be added to Shopping Cart / Basket from any of the following pages,
Shopping Template
Quick Order form
Upload Order file
Normal Product Catalog page (Product listing page)
I would like to track from which Page(s) mentioned above, product gets added to the Shopping Cart and eventually Order gets placed, How can I achieve this?
Analytics script
<script type="text/javascript">
/* Google Analytics */
var googleAnalyticsTrackingId = 'UA-xxxxx-x';
var _gaq = _gaq || [];
_gaq.push(['_setAccount', googleAnalyticsTrackingId]);
var dimensionValue = '1234';
_gaq.push('set', 'dimension2', dimensionValue);
_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>

Event Tracking - Web Tracking (ga.js) not working on Mobile

I am trying to capture an event when the user clicks a link on my website.
I have added the tracking code at start of <body> tag
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-64301593-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>
and then I am using
onclick="_gaq.push(['_trackEvent', 'landing_page', 'i_am_interested_click']);
on the <a> tag where I want to track an event.
It is working perfectly fine for all browsers on Desktop and I am able to see the events on Google Analytics Realtime -> Events tab. However same thing does not work for any browsers on mobile (I have tried Chrome and Firefox).
I have another hyper-link on the same page where I am tracking another event, this time by creating a jquery listener for the on-click event. However, that also does not work for mobile browsers (works on Desktop browsers).
The jquery code is as below:
$("#contact_me_button").click(function(){
$(this).fadeOut("slow", function() {
$(this).html("<a href='tel:+918695707070'>+918695707070</a>");
$(this).fadeIn("slow");
});
fbq('track', 'Lead');
_gaq.push(['_trackEvent', 'landing_page', 'call_attempted']);
});
Need help as I am new to this.
Thanks in advance !

Using google analytics event tracking on 2 pages

Im working on setting up event tracking on a website, the user visits index.php, where he completes a quizz built with javascript, after answering each question i track the event with something like :
_gaq.push(['eventTracker._trackEvent', "myCategory", question_number.toString()]);
After the user completes the quizz he's redirected to another page : result.php where i track the landing event with :
_gaq.push(['eventTracker._trackEvent', 'myResult', myScore.toString()]);
The problem is that on analytics i can see the events with category = "myCategory" but not events with category "myResult".
Here is the code (this part is present in both index.php and result.php):
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['_trackPageview']); //an account for tracking page views
(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>
<script>
//the account we attach events to
_gaq.push(['eventTracker._setAccount', 'UA-XXXXXXX-3']);
//..... racking code as posted before

Uncaught ReferenceError: _gaq is not defined (Google Analytics)

The following message appears when viewing a site page in the chrome debug log.
Uncaught ReferenceError: _gaq is not defined
The page itself is supposed to track an object using the onload event handler and fire a _trackEvent for Google Analytics.
My best guess is that perhaps the ga.js file doesn't load in time and therefore the onload _trackEvent triggered is not caught. the async snippet is being used before the </body> close and the object is positioned in the middle <body>.
(some other posts have referenced jQuery position too but, this might be a red herring)
any assistance greatly appreciated.
From https://developers.google.com/analytics/devguides/collection/gajs/ this code replaces your existing "traditional snippet" with the "latest, asynchronous version, you should remove the existing tracking snippet first."
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_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>
To see your events come in (to know if this is working) look for "Events" under the "Real-Time" menu option on the left side of the "Reporting" page.
Regarding the position of the async snippet, the GA help page says -
Paste this snippet into your website template page so that it appears
before the closing </head> tag.
My first thought was that JS should be loaded at the bottom of the page to improve page speed. However, the GA async tracking snippet should be loaded in the head, as it will not load the ga.js immediately, and it won't block page execution.(It does this by dynamically adding the script tag to the DOM, which puts it at the back of the queue.)
If, for some reason, you can't move the async snippet to the head, you can define _gaq yourself, like this-
<button onclick="var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>
Had the same problem. You have to define the _gaq array. Just add this after your Google Analytics script in the header:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
You can use the last version of analytics.js instead of ga.js
ga.js is a legacy library. If you are starting a new implementation we
recommend you use the latest version of this library, analytics.js.
For exisiting implementations, learn how to migrate from ga.js to
analytics.js.
Here is an example:
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'cats.mp4'
});
Change the Tracking Code to:
<script type="text/javascript">
var gaq;
var _gaq = gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'yourdomain.com']);
_gaq.push(['_setAllowLinker', true]);
_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>

jQuery to track Google analytics events not working

I'm trying to use google anayltics events but so far without any success..
What I'm doing is loading 5 pages using jQuery load function
and I want to track the "Next button" for each load. but looks like i'm doing something wrong..
This is the next button event code:
$('.NextButton').click(function () {
_gaq.push(['_trackEvent', 'fz_main_page', 'clicked']);
installation.load_page({ inner_page: next_page, comid: data.comid, outerid: data.outerid, single_app: "1" });
});
Analytics Code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25162785-2']);
_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>
What am I doing wrong?
It's possible that the installation.load_page function is preventing the trackEvent from firing. Try wrapping your load function in a setTimeout:
setTimeout('installation.load_page({ inner_page: next_page, comid: data.comid, outerid: data.outerid, single_app: "1" })', 100);
Install the Google Analytics Debugger. Look in the console (control, shift, j) for the event tracking processing.
If you don't see all of your events tracking there, then something else is up with the tracking code.

Resources