1

i have a problem with PHP and Google Analytics. Basically what i want to do is to count a visit when the user access this image from my email <img src='path/to/image' /> from his email client in in Google Analytics.

// If is a view then just show an image
if($mysqlAction == "VIEW")
{
    // Create a blank image and add some text
    header('Content-Type: image/jpeg');
    readfile('image.jpg');
    ?>
    <script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-35932495-2']);
    _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>
    <?php
}

My problem is that after

    // Create a blank image and add some text
    header('Content-Type: image/jpeg');
    readfile('image.jpg');

it is useless to put any code, because the output of script.php will be like accessing an image.jpg , but i really need to count views in google analytics.

What i want :

  1. How can i solve this?
  2. Don't give me an alternative like TrackReports from Sendblaster ... or any similar solution.

My ideas are :

  1. My idea was something with double buffer like first show the analitycs code and then ob_flush() the image
  2. Fetch with CURL another page or something ...
  3. Use Analitics for PHP ...

Any solution?

4

2 回答 2

0

您可以使用广告系列跟踪标记您的超链接(请参阅http://cutroni.com/blog/2008/11/04/email-tracking-with-google-analytics/),但除此之外,我不知道您是如何可以做到这一点。在最好的情况下,电子邮件客户端中对 JS 的支持是不稳定的,当然,您不能在图像中包含 JS。我的建议只是在您的电子邮件中标记您的超链接,至少这样您就可以在 Analytics 内部知道有多少访问者来自您的电子邮件活动。

如果您想跟踪这些电子邮件中有多少被阅读等,那么它们可以在您当前的方法中使用(php 空白图像)。但是,您随后将无法将此数据发送到 Analytics,您必须开发自己的数据使用方式。

话虽如此,如今没有人被迫下载这些图像,因此您获得的指标充其量只是估计值。

于 2012-11-01T13:17:25.337 回答
0

我想你可以在这篇文章中找到解决方案:http ://techpad.co.uk/content.php?sid=186

作者实现 PHP 类从服务器端而不是客户端 JS 访问谷歌分析服务。

我希望它会有所帮助。

于 2012-11-01T13:11:17.987 回答