LoginSignup
9
9

More than 5 years have passed since last update.

【GoogleAnalitycs】ガラケーでユニバーサルアナリティクスのページビューカウントを取得する【ガラケー】

Last updated at Posted at 2014-10-03
$cid = 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 )
);

$data['v']   = '1';
$data['tid'] = 'UA-xxxxx-x'; // UAのコード
$data['cid'] = $cid;
$data['t']   = 'pageview';
$data['dh']  = $_SERVER['HTTP_HOST'];
$data['dp']  = $_SERVER['REQUEST_URI'];
$data['dt']  = 'PageTitle'; // ページのタイトル


$url = 'http://www.google-analytics.com/collect'; // This is the URL to which we'll be sending the post request.
$content = http_build_query($data); // The body of the post must include exactly 1 URI encoded payload and must be no longer than 8192 bytes. See http_build_query.
$content = utf8_encode($content); // The payload must be UTF-8 encoded.
$user_agent = $_SERVER['HTTP_USER_AGENT']; 

$url  = 'http://www.google-analytics.com/collect';
$url .= '?v='.$data['v'];
$url .= '&tid='.$data['tid'];
$url .= '&cid='.$data['cid'];
$url .= '&t='.$data['t'];
$url .= '&dh='.$data['dh'];
$url .= '&dl='.$data['dl'];
$url .= '&dr='.$data['dr'];
$url .= '&dp='.$data['dp'];
$url .= '&dt='.$data['dt'];

で、ページフッターあたりに

<img src="<?php echo $url;?>">

9
9
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9