DIJ
DIJ

Reputation: 397

Google Analytics: track links from email to external page

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

  1. the url looks nicer and
  2. 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?

Upvotes: 1

Views: 2918

Answers (3)

selitsky
selitsky

Reputation: 44

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.

Upvotes: 2

DIJ
DIJ

Reputation: 397

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/

Upvotes: 1

Ragnar
Ragnar

Reputation: 543

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 :)

Upvotes: 1

Related Questions