PHP谷歌分析跨多个域的跟踪

时间:2014-11-26 12:11:43

标签: php google-analytics

如何跟踪两个域之间用户的移动? 方案:

1)用户点击 doman-exemple1.com 中的按钮,然后从doman-exemple1.com发出并转到doman-exemple2.com

<a href="http://doman-exemple2.com/?_ga=1.214541711.9842898548.1417191250" rel="nofollow">  login</a>

2) doman-exemple2.com 仅在服务器上执行(不可能使用javascript)

<?php 
// executed something codes... ;
// go with result to doman-exemple1.com
header('Location: http://doman-exemple1.com/?good='.$code);
?>

3)用户返回http://doman-exemple1.com/?good=t45ygsw45t4

我需要谷歌分析了解同一个用户而不是不同的用户

这有效: https://support.google.com/analytics/answer/1034342?hl=en

但是我不能使用thit因为 doman-exemple2.com 只执行PHP并且无法使用javascript

请帮助如何在没有javascript且仅使用PHP的情况下执行此操作

P.S。 我读到这个:https://github.com/thomasbachem/php-ga但不明白我是否可以在我的情况下使用它

2 个答案:

答案 0 :(得分:1)

如果您使用的是GA的通用分析,请查看“测量协议”。 https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

这可以让您仅通过PHP进行跟踪,并且通过使用相同的cid,您可以继续跟踪(使用相同的配置文件)会话。

答案 1 :(得分:0)

thenx for Marcel Dumont

代码来自此示例:http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/

<强> doman-exemple1.com:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  //https://support.google.com/analytics/answer/1034342?hl=uk
  //https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain
  ga('create', 'UA-33396333-20', 'auto', {'allowLinker': true}); 
  ga('require', 'linker'); 
  ga('linker:autoLink', ['doman-exemple2.com'] );
  ga('send', 'pageview');
</script>

<强> doman-exemple2.com:

     <?php    
        if (isset($_GET["_ga"])) {
        setcookie('_ga_my', $_GET["_ga"],time()+3600*24*24);
        list($version,$domainDepth, $cid1, $cid2) = split('[\.]', $_GET["_ga"],4);
        $contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
        $cid = $contents['cid'];
        $ga = "GA1.2.".$cid;
        setcookie('_ga', $ga,time()+3600*24*24);
        } 


    function gaParseCookie() {
    if (isset($_COOKIE['_ga'])) {
        list($version,$domainDepth, $cid1, $cid2) = split('[\.]', $_COOKIE["_ga"],4);
        $contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
        $cid = $contents['cid'];
      }
      else $cid = gaGenUUID();
      return $cid;
    }

    // Generate UUID v4 function - needed to generate a CID when one isn't available
    function gaGenUUID() {
      return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
      );
    }

    //echo gaParseCookie();


    function gaBuildHit( $method = null, $info = null ) {
      if ( $method && $info) {

      // Standard params
      $v = 1;
      $tid = "UA-33396333-20"; // Put your own Analytics ID in here
      $cid = gaParseCookie();

      // Register a PAGEVIEW
      if ($method === 'pageview') {

        // Send PageView hit
        $data = array(
          'v' => $v,
          'tid' => $tid,
          'cid' => $cid,
          't' => 'pageview',
          'dt' => $info['title'],
          'dp' => $info['slug']
        );

        gaFireHit($data);
      } // end pageview method
     }
    }
    // See https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
    function gaFireHit( $data = null ) {
      if ( $data ) {
        $getString = 'https://ssl.google-analytics.com/collect';
        $getString .= '?payload_data&';
        $getString .= http_build_query($data);
        $result = file_get_contents( $getString );

        #$sendlog = error_log($getString, 1, "ME@EMAIL.COM"); // comment this in and change your email to get an log sent to your email

        return $result;
      }
      return false;
    }

    $data = array(
      'title' => 'Login doman-exemple2.com',
      'slug' => 'doman-exemple2.com'
    );
    gaBuildHit( 'pageview', $data);


$secrret="111";
    if (isset($_COOKIE['_ga_my'])) {
      $gogl='&_ga='.$_COOKIE['_ga_my'];
    header('Location: https://doman-exemple1.com/mass-add-email?utm_source=good&secret='.$secrret.'&utm_medium=good&utm_campaign=good'.$gogl);
    } else {
    header('Location: https://doman-exemple1.com/mass-add-email?utm_source=good&secret='.$secrret.'&utm_medium=good&utm_campaign=good');
    }
    exit();

        ?>
相关问题