Ne Kr Ne Kr - 3 months ago 33
Javascript Question

redirect with javascript and analytics

I would like to know how I can track a link from an email with google analytics.

In the email there is a link to a pdf ebook confirmation page. In need to track that, so when a person click the link to the ebook, I need to take the URL parameters, and redirect with javascript to the confirmation page, after google analytics have registrered the tracking.

But how can I take the URL parameters and redirect with javascript? Is it something to do with

window.location
?

Answer

It would make sense to want this, if the destination URL was to a static PDF file that would be served directly by the HTTP server (no application involved). For example, http://example.com/ebook.pdf. Since the PDF is a static file, there is no way to inject the Google Analytics (GA) Javascript code to register the page view. To track the click in the email link using UTM codes and register the page view with GA, you would need an intermediate page that contains your GA javascript and pageView call and then use Javascript to redirect to the static PDF URL.

The code to this intermediate page would be something like this:

<html>
<head>
  <!-- Insert your Google Analytics code here; this must come first -->
  <script type="text/javascript">
    var ebook_url = "http://example.com/ebook.pdf";
    window.location = ebook_url; // redirect happens here
  </script>
</head>
<body></body>

Let's assume that intermediate redirect page is served at http://example.com/ebook/confirmation. In the email you would add the UTM codes to that URL link, like @yuriy636 suggested: http://yourwebsite.com/ebook/confirmation?ut‌​m_medium=email&utm_c‌​ampaign=ebookdownloa‌​d

Comments