Cardinal Path’s response to COVID-19 Cardinal Path is sharing all we know to help marketers during COVID-19.  Learn more.

Back in January 2009, I wrote a post on how to track traffic from Press Releases in Google Analytics. That posts referenced the old synchronous version of the Google Analytics tracking code and needed a code update with the most recent tracking code release by Google, featuring asynchronous code execution.

Please note that I am not going to re-do the entire post, just the code portion.

So here is the code update:

Traditional Snippet (Synchronous):

<script type="text/javascript">
var srcPage = getDomain (document.referrer);
var parameter = get_parameter('id');

function get_parameter(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&#]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
    if( results == null )
      return "";
    else
      return results[1];
}

function getDomain (thestring) {
  var urlpattern = new RegExp("(http|ftp|https)://(.*?)/.*$");
  var parsedurl = thestring.match(urlpattern);
      if( parsedurl == null )
      	return "";
      else
        return parsedurl[2];
}

if (parameter == '1')
{
    window.location.hash = "utm_source="+srcPage+"&utm_medium=press_release&utm_campaign=hurricane_katrina";
}
</script>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}
</script>

Asynchronous Snippet (NEW):

<script type="text/javascript">
var srcPage = getDomain (document.referrer);
var parameter = get_parameter('id');

function get_parameter(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&#]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
    if( results == null )
      return "";
    else
      return results[1];
}

function getDomain (thestring) {
  var urlpattern = new RegExp("(http|ftp|https)://(.*?)/.*$");
  var parsedurl = thestring.match(urlpattern);
      if( parsedurl == null )
      	return "";
      else
        return parsedurl[2];
}

if (parameter == '1')
{
    window.location.hash = "utm_source="+srcPage+"&utm_medium=press_release&utm_campaign=hurricane_katrina";
}
</script>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
  _gaq.push(['_setAllowAnchor', 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>

Related Posts:

Sharing is caring!

Popular

COVID-19 Crisis Navigator​

In partnership with Dentsu, Cardinal Path helps you distill the overwhelming news and information into a bi-weekly report highlighting emerging trends and insights during the pandemic.

EXPLORE THE REPORT

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Success!
Your message was received.

Thank you.

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Thank you.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Click here to download access the tool.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Thank you for submitting the form.

Thank you for submitting the form.

Cardinal Path hosted a live session to connect with you and answer all your questions on Google Analytics.
Get all the expertise and none of the consultancy fees in this not-to-be-missed, rapid-fire virtual event.

Thank you for registering.

Cardinal Path is continuing with its series of free training. Next we are conducting training on Google Data Studio. Check it out here.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for your submission.

Message Sent

Thank you for registering.

You will receive a unique link to view the webinar via email.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Success! Thank you
for reaching out.