Your Ad Here

Posted By

theroamingcoder on 09/22/09


Tagged

php google Analytics pregreplacecallback


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

pster
BrianCoyDesign
satsuma
timpreneur


Add google analytics tag to all links in email body


 / Published in: PHP
 

This code can be used to add Google Analytics tracking tags to all links back to your site in email messages that you send. You can then track the referrals back to your site from those emails. See http://www.google.com/support/analytics/bin/answer.py?answer=55578&hl=en for a tool to help you build the tags.

  1. $email_body ="<a href='http://www.mysite.com/specials/?q=daily'>Args already present</a><br/>
  2. <a href='http://www.mysite.com/specials/'>No args already present</a><br/>
  3. <a href='http://www.mysite.com/'>Just a domain</a><br/>
  4. <a href = 'http://www.mysite.com'>Just a domain with no trailing slash</a><br/>
  5. <a href='https://www.mysite.com/specials/?q=daily'>https version</a><br/>
  6. ";
  7.  
  8. $email_body = preg_replace_callback(
  9. '@href\s?=\s?(\'|")(https?://www.mysite.com(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)(\\1)@',
  10. '$matches',
  11. '$ga_tags = "utm_source=mysite&utm_medium=email&utm_term=spam&utm_content=lotsofit&utm_campaign=lotsofspam";
  12. return $matches[6] ? "href=".$matches[1].$matches[2]."&".$ga_tags.$matches[1] : "href=".$matches[1].$matches[2]."?".$ga_tags.$matches[1];'
  13. ),
  14. $email_body
  15. );
  16.  
  17. print $email_body;

Report this snippet  

You need to login to post a comment.