memo

memo dayo.

PHP4.4.4環境でGoogle Analytics Mobileを利用する

★自前の関数でHTTP GETリクエストを送信するVer.
※正確じゃなくてもいいから動けばイイや。


【ga.phpの修正】
■setrawcookieを置換
・置換前

setrawcookie

・置換後

setcookie


■自前の関数を追加
・新規追加

<?php
// ==============================================
function ga_get_contents ($str_url, $ary_opt) {
	// ---- define ----
	$port    = 80;
	$timeout = 29;

	// ---- init ----
	$str_ret = '';
	$err_num = '';
	$err_str = '';
	$ary_url = parse_url($str_url);

	// ---- check param ----
	if (is_array($ary_opt) === false) {
		$ary_opt = array();
		$ary_opt['http'] = array();
	}
	if (isset($ary_opt['http']['method']) === false) {
		$ary_opt['http']['method'] = 'GET';
	}
	if (isset($ary_opt['http']['user_agent']) === false) {
		$ary_opt['http']['user_agent'] = 'PHP';
	}
	if (isset($ary_opt['http']['header']) === false) {
		$ary_opt['http']['header'] = 'Accepts-Language: ja-jp';
	}
	if (is_array($ary_url) === false) {
		$ary_url = array();
	}
	if (isset($ary_url['host']) === false) {
		$ary_url['host'] = '';
	}
	if (isset($ary_url['path']) === false) {
		$ary_url['path'] = '';
	}
	if (isset($ary_url['query']) === false) {
		$ary_url['query'] = '';
	}

	// ---- connect ----
	$fp = fsockopen($ary_url['host'], $port, $err_num, $err_str, $timeout);
	if ($fp !== false) {
		// ---- make request ----
		$str_out = '';
		$str_out .= $ary_opt['http']['method'] . ' ' . $ary_url['path'] . '?' . $ary_url['query'] . ' HTTP/1.1' . "\r\n";
		$str_out .= 'Host: ' . $ary_url['host'] . "\r\n";
		$str_out .= 'User-Agent: ' . $ary_opt['http']['user_agent'] . "\r\n";
		$str_out .= $ary_opt['http']['header'] . "\r\n";
		$str_out .= 'Connection: Close' . "\r\n";
		$str_out .= "\r\n";

		// ---- put request ----
		fwrite($fp, $str_out);
		while (!feof($fp)) {
			$str_ret .= fgets($fp, 347);
		}

		// ---- disconnect ----
		fclose($fp);
		unset($str_out);
	}
	unset($fp);
	unset($err_num);
	unset($err_str);
	unset($ary_url);

	// ---- trim header ----
	if (strpos($str_ret, "\r\n\r\n") !== false) {
		$ary_tmp = explode("\r\n\r\n", $str_ret);
		if (count($ary_tmp) > 1) {
			array_shift($ary_tmp);
			$str_ret = implode("\r\n\r\n", $ary_tmp);
		}
		unset($ary_tmp);
	}

	return $str_ret;
}
// ==============================================


■file_get_contentsを置換
・置換前

file_get_contents($utmUrl, false, stream_context_create($options))

・置換後

ga_get_contents($utmUrl, $options)


■結果
ラッキングはされているみたい。




PEARで差を埋めるVer.
PEARインストール】

# by root
./pear install PHP_Compat-alpha


【ga.phpの修正】
PEARの読み込みを追加
・新規追加

require_once('PHP/Compat/Function/setrawcookie.php');
require_once('PHP/Compat/Function/file_get_contents.php');


■setrawcookieを置換
・置換前

setrawcookie(
	COOKIE_NAME,
	$visitorId,
	$timeStamp + COOKIE_USER_PERSISTENCE,
	COOKIE_PATH
);

・置換後

setrawcookie(
	 COOKIE_NAME
	,$visitorId
	,$timeStamp + COOKIE_USER_PERSISTENCE
	,COOKIE_PATH
	,''
	,False
	,False
);


■file_get_contentsを置換
・置換前

file_get_contents

・置換後

php_compat_file_get_contents


■結果
試した環境では、
1回の画面表示で、「__utm.gif」へのリクエストが
2回(パラメータなし、あり)送信されて、
GoogleAnalyticsのトラッキングはされなかった。