Reputation: 1012
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
<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)
<?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
I need that google analytics understand that this same user and it not different users
This works: https://support.google.com/analytics/answer/1034342?hl=en
But I cannot use thit becouse doman-exemple2.com executed only PHP and cannot use javascript
please help how to do this without javascript and only with PHP
P.S. I read this: https://github.com/thomasbachem/php-ga but not understand if i can use this in my situations
Upvotes: 0
Views: 517
Reputation: 1012
thenx for Marcel Dumont
code get from this example: 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, "[email protected]"); // 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();
?>
Upvotes: 0
Reputation: 1207
If you are using Universal Analytics of GA, then have a look at the Measurement Protocol. 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.
Upvotes: 1