Google Analytics: track links from email to external page - google-analytics

Google Analytics is driving my crazy.
I have an app in the app store. I'd like to sent an email linking direct to my app's page in the app store, BUT i'd like to track the clicks.
So far, i have a link like
www.mypage.com/promoletter/
That location contains an index.php file wich does a 302 redirect to my own site as follows:
header("Location: http://www.mypage.com/index.php?page=App_Store
&utm_source=promoletter&utm_medium=email&utm_campaign=v1-promoletter");
I do it this way with 2 redirect because
the url looks nicer and
I can still edit the utm variables after the newsletter has been send
Now, through my index.php page, a html page is created which does an other 302 redirect to
http://itunes.apple.com/ie/app/MY-APP/id-SO-AND-SO?ls=1&mt=8
also using the php header function as above.
Now, everything works great, but of course it's not being recorded in Analytics (hence the question :) ). Writing this, I realize this is because the ga.js file is never loaded because you cannot have any output before the php header function.
How to solve?

Simple code without timeout is:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = false;
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);
})();
_gaq.push(function(){
window.location.replace("http://itunes.apple.com/ie/app/MY-APP/id-SO-AND-SO?ls=1&mt=8");
});
</script>
There is no need to wait, just setup ga.async = false; and redirect work faster, with little delay.

The best way to solve this is to use a javascript redirect. Send out a html page with the ga.js and a script tag:
...
<script type="text/javascript" src="ga.js"></script>
<script type="text/javascript">
window.location.href = "http://itunes.apple.com/ie/app/MY-APP/id-SO-AND-SO?ls=1&mt=8http://itunes.apple.com/ie/app/MY-APP/id-SO-AND-SO?ls=1&mt=8"
</script>
...
Or to make sure the ga.js is loaded:
...
<script type="text/javascript" src="ga.js"></script>
<script type="text/javascript">
setTimeout('window.location.href = "http://itunes.apple.com/ie/app/MY-APP/id-SO-AND-SO?ls=1&mt=8http://itunes.apple.com/ie/app/MY-APP/id-SO-AND-SO?ls=1&mt=8"',500)
</script>
...
I'm not sure if it's possible to "spoof" the ga pixel request. You might as well need cookies that are not available for you. But else you might be able to fake a ga pixel from your backend to track the correct values.
The javascript redirect might though be a bit more simple :)

Thanks Ragnar,
You pointed me in the right direction and I've learned a lot since posting my question.
Maybe somebody else is interested in my actual solution or somebody has a better way.
I've solved my problem using 3 redirects (I don't see another way unfortunately).
First, I've got a link like:
www.mypage.com/promoletter/
This than redirects to (php header function):
header("Location:
http://www.mypage.com/index.php?page=processGAVars&nextPage=App_Store
&utm_source=promoletter&utm_medium=email&utm_campaign=v1-promoletter");
The constructed page is processing the Google Analytics variables and once again redirects to another page (nextPage=App_Store) this time using javascript or meta refresh when javascript is disabled.
You could redirect straight to the App Store, but than the user is left with a URL in it's address bar containing your GA variables (apparently only on iPhone/iPad, see below).
There is a short delay before redirection so GA gets a change to do it's job (race condition).
$url = "http://www.mypage.com/index.php?page=".$_GET['nextPage'];
<noscript>
<meta http-equiv="refresh" content="1; url=<?php echo $url;?>" />
</noscript>
<script type="text/javascript">
function redirect() {
window.location.replace("<?php echo $url;?>");
}
</script>
<body onload="setTimeout('redirect()', 500)">
The last page does the actual redirect to the App Store again using javascript or meta refresh just like above only this time without any delays.
So far, in the browsers I have tested (firefox with and without javascript, chrome, safari and on iPhone/iPad safari and terra) there is a short delay and the user can see the redirect. However, this redirects doesn't show up in the users history and pressing the back button once returns the te previous page.
On a computer the user, gets the itunes.apple.com page. On an IOS device, the App Store app is launched and the browser displays the page the user left.
I have found that on an iDevice, including the GA javascript in the page that does the actual redirecting to the iTunes page, blocks the process of going back to the originating page (e.g. stackoverflow.com). The user is than presented the (blank) redirection page.
See it in action?
http://hd-apps.com/links/stackoverflow/

Related

How to make all the web page within iframe open in same tab?

I have an iframe within which I open a third party website. Few links in the webpage opens in new tab. How do I override this option and make sure all the links open in the same iframe?
Sample Code
Your browser doesn't support iframes
This code can't be tested on an offline page. You will need to test it within live page
Suppose this is your iframe.
<iframe src='give_your_url_here.com'/>
Put the below code instead of your iframe and replace the variable 'url' with your required url.
This is not a proper method, but this is the only solution to your situation.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<div id="queryResultContainer"/>
<script type="text/javascript">
var url = 'give_your_url_here.com';//without http://
$.getJSON('http://anyorigin.com/go?url=' + url + '&callback=?', function (data) {
var aa = data.contents;
$('#queryResultContainer').html(aa + "<script> setInterval(function () { $('#queryResultContainer').find('a').removeAttr('target');}, 200);</script" + ">");
});
</script>
If your site is HTTPS then change
$.getJSON('http://anyorigin.com....
To
$.getJSON('https://anyorigin.com....
You can't. If the links on the third party website are programmed to open in new windows / tabs, you have no way of modifying that code.

Google tag manager don't do analytics

I have Google tag manager tag inserted on all pages on the website.
head:
<!-- Google Tag Manager -->
<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-XXXXX');
</script>
<!-- End Google Tag Manager -->
At the end of body:
<noscript>
"<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MDKNR5" height="0" width="0" style="display:none;visibility:hidden"></iframe>"
</noscript>
On homepage and four other pages on website, this code works OK
On those pages, gtm head element (I suppose), creates two new lines above him.
<script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script>
<script async="" src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX"></script>
But on two remaining pages, only one line is created
<script async="" src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX"></script>
I went deep in that gtm.js file and found:
if (!Ck) {
var Z = a[oc] ? "u/analytics_debug.js" : "analytics.js";
a[kd] && !a[oc] && (Z = "internal/" + Z);
Ck = !0;
r(L("https:", "http:", "//www.google-analytics.com/" + Z, u), function() {
x[l()].loaded || d()
}, d)
}
That returns false, and don't do that part of code. So, I don't have analytics on that page.
Chrome extension "Google Tag Assistant" gives me this notification:
This page did not send a hit, but other recorded pages on the same
domain did send hits. Most likely, this page was not properly tagged
with the Google Analytics tracking code
In the end, to mention, I don't have access to that Google account, I only got tracking codes for GTM and analytics.
If the tracking code for Google Analytics should fire or not is set within the GTM interface. If, for some reason, there is a blocking trigger set to not fire Analytics on those pages, that is the most likely reason. It is basically impossible to answer your question without access to the configuration itself or more information from the GTM account and the way it is set up.

Google Universal Analytics custom dimensions and trackpageview

I'm trying to migrate from Google old Analytics to Universal Analytics. I have code below and from universal - developers guide I couldn't find solution.
Inside my analytics code I had these lines. Part 1:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-1']);
_gaq.push(['_trackPageview','/tools/one');
_gaq.push(['_setCustomVar', 1, 'name', 'michael', 1]);
(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);
})();
Also inside my some other script I have this code snippet. Part 2:
if (typeof _gaq !== "undefined" && _gaq !== null) {
_gaq.push(['_trackPageview', '/dosomework']);
}
How can I convert "gaq.push"s inside 2 parts and add to my universal analytics code ?
Unfortunately, migrating to analytics.js isn't as simple as just changing the code syntax.
Firstly you should know that Universal Analytics is currently in open beta phase. Currently google does not offer a way to "upgrade" or "convert" an existing web property to take advantage of universal analytics (analytics.js) tracking. You will need to setup a new web property (or new account) and check the "universal analytics" radio button.
Google currently recommends setting up analytics.js code in addition to your current ga.js code. Once you are happy that the base data is lining up between the two, you can either keep both versions on your page or decide on a date to remove the old ga.js code. The historical data in your old profile will still be there but it won't be tied to the new web property. I don't know if Google will eventually offer an "upgrade" or "convert" feature for existing ga.js based web properties; so far I have not seen any news on if/when they will offer this.
Moving on to Universal Analytics (analytics.js) code ...
Universal Analytics does not use the .push syntax. Instead, it has a function ga() that requires arguments to be passed to it. The first argument is the "command" argument, and the additional arguments are for passing additional settings, values, etc. based on the command.
setting the GA account is now done with the 'create' command
tracking a page view is now done with with the 'send' command
setting a custom variable* is now done as either an argument in the 'send' command (for only popping it on that 'send' command), or with the 'set' command (for setting it to be popped with all 'send' commands executed on the page)..but about this...
Custom Variables no longer exist
Well they do, but how they are implemented is different. Universal Analytics offers custom dimensions and metrics. Custom Variables are mostly what Custom Dimensions now are. The main difference is that setting things like the name and scope of the variable is now done within the GA interface instead of as an argument to the function. Also, you get more than 5 to work with now. To set this up, click on the web property you created, and you should see tabs
Profiles Tracking ..Custom Definitions
Click on the Custom Definitions tab to setup your custom dimensions and metrics there.
Now on to the page code
This is what the "equivalent" of the code you posted would look like:
First snippet:
<!-- Google Analytics -->
<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-123456-1');
ga('send', 'pageview', '/tools/one');
ga('set', 'dimension1', 'michael');
</script>
<!-- End Google Analytics -->
note: as mentioned above, you would set the name and scope of the dimension within the interface. 'dimension1' should be changed to whatever dimension# you created.
Second snippet:
if (typeof ga == 'function') {
ga('send', 'pageview', '/dosomework');
}
sidenote: Not really related to your question, but in your code you first send a page view and then set the custom variable. In case you didn't know, if you set the custom variable (_setCustomVar) after the page view (_trackPageview), your custom variable will not be sent with that page view (the '/tools/one' hit). It will (assuming your 2nd snippet gets popped later on) be sent along with that 2nd page view (the '/dosomework' one). Not sure why you would have two separate pageviews or if you knew about that order of operations thing but if you're happy with how things currently look in the reports..well the analytics.js version will behave the same way.

Custom reports showing download events in Google Analytics

Has anyone been able to include downloads within a custom report in Google analytics? I know I can get general/all events, but I just want downloads.
Thanks
If you are asking to track the downloads then you can add _trackEvent method in to the link that you want to track, like
Click <a onclick="_gaq.push(['_trackEvent', 'Downloads', 'Demo', this.href]);" href="downloads/google_dynamic_map.zip">here</a> to download with demo.
Above code is from my working site and I'm able to track download of that file.
To view the report you can select the Contents->Events menu from the left nav bar of your google analytics page.
In my link Downloads is the Event Category and Demo is the Event Action and this.href is my Event Label which gives me the url of the file.
I've also created a custom report only to track Downloads event category from Google's custom report option and can track it from that custom report, filtered by Downloads.
Screenshot of Custom Report settings
Screenshot of standard report and viewing from content menu
Useful links here: Link One and Link Two and Event Tracking Guide.
Update: Also remember it'll take some time after you setup all of those things to track and see the result.
Google Analytics on Steroids offers a great _gasTrackDownloads hook in their API if you don't already have the tracking code in place per #Sheikh's more accurate Custom Report answer above.
Code Example:
<script type="text/javascript">
var _gas = _gas || []; // _gas instead of _gaq
_gas.push(['_setAccount', 'UA-YYYYYY-Y']); // REPLACE WITH YOUR GA NUMBER
_gas.push(['_setDomainName', '.mydomain.com']); // REPLACE WITH YOUR DOMAIN
_gas.push(['_trackPageview']);
_gas.push(['_gasTrackForms']);
_gas.push(['_gasTrackOutboundLinks']);
_gas.push(['_gasTrackMaxScroll']);
_gas.push(['_gasTrackDownloads']); // track downloads
_gas.push(['_gasTrackYoutube', {force: true}]);
_gas.push(['_gasTrackVimeo', {force: true}]);
_gas.push(['_gasTrackMailto']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = '//cdnjs.cloudflare.com/ajax/libs/gas/1.10.1/gas.min.js'; // new url
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
Link: Google Analytics on Steroids

Concrete5 google analytics

How do i get google analytics to work
I've put it in site/settings - tracking code.
I made a google analytics account but it says 0 visited
Hope you guys can help
Best Regards
It takes one day before you will see the result in Google Analytics. It is not real time... If you placed the tracking code on your website you should be good to go.
Place the following script immediatly after your <body> tag.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<<Your UA-000909 ID>>']);
_gaq.push(['_trackPageview']);
</script>
Place the following script immediately before your </body> tag.
<script type="text/javascript">
(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';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>
Between the scripts your content should be. This gives the best page load time.
You can test whether Google Analytics is configured correctly immediately after setup by utilizing the Google Analytics realtime view. In one browser tab visit your website, in another tab have Google Analytics open, with the real time tab selected (it can be found on the left hand side of the Google Analytics user interface under the reporting tab).
To install, grab your tracking code (it sounds like you've already done this) and paste before the closing tag.
You can also test whether you've configured Google Analytics correctly by selecting admin, then tracking info, then tracking code. If configured correctly, you should see the Status as "Status: Receiving Data".

Resources