简体   繁体   English

PHP Google Analytics(分析)跨多个域的跟踪

[英]PHP google analytics Tracking across multiple domains

How to track the movement of the user between the two domains? 如何跟踪用户在两个域之间的移动? scheme: 方案:

1) the user clicks the button in doman-exemple1.com and go out from doman-exemple1.com and comes to doman-exemple2.com 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 executed only on the server (that is not possible to use javascript) 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) user returned to the http://doman-exemple1.com/?good=t45ygsw45t4 3)用户返回到http://doman-exemple1.com/?good=t45ygsw45t4

I need that google analytics understand that this same user and it not different users 我需要Google Analytics(分析)了解该用户,而不是其他用户

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

But I cannot use thit becouse doman-exemple2.com executed only PHP and cannot use javascript 但是我不能使用它,因为doman-exemple2.com仅执行PHP并且不能使用javascript

please help how to do this without javascript and only with PHP 请帮助如何在没有JavaScript的情况下(仅在PHP中)

PS I read this: https://github.com/thomasbachem/php-ga but not understand if i can use this in my situations PS我读到这个: https//github.com/thomasbachem/php-ga但不明白我是否可以在我的情况下使用它

If you are using Universal Analytics of GA, then have a look at the Measurement Protocol. 如果您使用的是通用Analytics(分析),请查看Measurement Protocol。 https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

That can allow you to track through PHP only, and by using the same cid you can continue to track (using the same profile) the session. 这样就可以只跟踪PHP,并且通过使用相同的cid,您可以继续跟踪(使用相同的配置文件)会话。

thenx for Marcel Dumont 马克斯·杜蒙

code get from this example: http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/ 代码可从以下示例获取: http : //www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/

doman-exemple1.com: 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: 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();

        ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM