php 移动网站添加 google Analytics统计代码
来源:互联网 发布:网易闪电邮for mac 编辑:程序博客网 时间:2024/06/06 02:39
第 1 步:复制下列代码,然后将其粘贴到您要跟踪的每个网页的 <html> 标记之前,并使其紧邻此标记。
- <?php
- // Copyright 2009 Google Inc. All Rights Reserved.
- $GA_ACCOUNT = "MO-19479815-4";
- $GA_PIXEL = "/ga.php";
- function googleAnalyticsGetImageUrl() {
- global $GA_ACCOUNT, $GA_PIXEL;
- $url = "";
- $url .= $GA_PIXEL . "?";
- $url .= "utmac=" . $GA_ACCOUNT;
- $url .= "&utmn=" . rand(0, 0x7fffffff);
- $referer = $_SERVER["HTTP_REFERER"];
- $query = $_SERVER["QUERY_STRING"];
- $path = $_SERVER["REQUEST_URI"];
- if (emptyempty($referer)) {
- $referer = "-";
- }
- $url .= "&utmr=" . urlencode($referer);
- if (!emptyempty($path)) {
- $url .= "&utmp=" . urlencode($path);
- }
- $url .= "&guid=ON";
- return str_replace("&", "&", $url);
- }
- ?>
第 2 步:复制下列代码,然后将其粘贴到您要跟踪的每个网页的 </body> 标记之前,并使其紧邻此标记。
- <?php
- $googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
- echo '<img src="' . $googleAnalyticsImageUrl . '" />';?>
第3步:
下载 ga.php 并将其保存到您的网络服务器的根目录 ("/")。请确保将您的根目录配置为执行服务器端代码
ga.php代码如下:
<?php
/**
Copyright 2009 Google Inc. All Rights Reserved.
**/
// Tracker version.
define("VERSION", "4.4sh");
define("COOKIE_NAME", "__utmmobile");
// The path the cookie will be available to, edit this to use a different
// cookie path.
define("COOKIE_PATH", "/");
// Two years in seconds.
define("COOKIE_USER_PERSISTENCE", 63072000);
// 1x1 transparent GIF
$GIF_DATA = array(
chr(0x47), chr(0x49), chr(0x46), chr(0x38), chr(0x39), chr(0x61),
chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x80), chr(0xff),
chr(0x00), chr(0xff), chr(0xff), chr(0xff), chr(0x00), chr(0x00),
chr(0x00), chr(0x2c), chr(0x00), chr(0x00), chr(0x00), chr(0x00),
chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x00), chr(0x02),
chr(0x02), chr(0x44), chr(0x01), chr(0x00), chr(0x3b)
);
// The last octect of the IP address is removed to anonymize the user.
function getIP($remoteAddress) {
if (empty($remoteAddress)) {
return "";
}
// Capture the first three octects of the IP address and replace the forth
// with 0, e.g. 124.455.3.123 becomes 124.455.3.0
$regex = "/^([^.]+\.[^.]+\.[^.]+\.).*/";
if (preg_match($regex, $remoteAddress, $matches)) {
return $matches[1] . "0";
} else {
return "";
}
}
// Generate a visitor id for this hit.
// If there is a visitor id in the cookie, use that, otherwise
// use the guid if we have one, otherwise use a random number.
function getVisitorId($guid, $account, $userAgent, $cookie) {
// If there is a value in the cookie, don't change it.
if (!empty($cookie)) {
return $cookie;
}
$message = "";
if (!empty($guid)) {
// Create the visitor id using the guid.
$message = $guid . $account;
} else {
// otherwise this is a new user, create a new random id.
$message = $userAgent . uniqid(getRandomNumber(), true);
}
$md5String = md5($message);
return "0x" . substr($md5String, 0, 16);
}
// Get a random number string.
function getRandomNumber() {
return rand(0, 0x7fffffff);
}
// Writes the bytes of a 1x1 transparent gif into the response.
function writeGifData() {
global $GIF_DATA;
header("Content-Type: image/gif");
header("Cache-Control: " .
"private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
header("Pragma: no-cache");
header("Expires: Wed, 17 Sep 1975 21:32:10 GMT");
echo join($GIF_DATA);
}
// Make a tracking request to Google Analytics from this server.
// Copies the headers from the original request to the new one.
// If request containg utmdebug parameter, exceptions encountered
// communicating with Google Analytics are thown.
function sendRequestToGoogleAnalytics($utmUrl) {
$options = array(
"http" => array(
"method" => "GET",
"user_agent" => $_SERVER["HTTP_USER_AGENT"],
"header" => ("Accepts-Language: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
);
if (!empty($_GET["utmdebug"])) {
$data = file_get_contents(
$utmUrl, false, stream_context_create($options));
} else {
$data = @file_get_contents(
$utmUrl, false, stream_context_create($options));
}
}
// Track a page view, updates all the cookies and campaign tracker,
// makes a server side request to Google Analytics and writes the transparent
// gif byte data to the response.
function trackPageView() {
$timeStamp = time();
$domainName = $_SERVER["SERVER_NAME"];
if (empty($domainName)) {
$domainName = "";
}
// Get the referrer from the utmr parameter, this is the referrer to the
// page that contains the tracking pixel, not the referrer for tracking
// pixel.
$documentReferer = $_GET["utmr"];
if (empty($documentReferer) && $documentReferer !== "0") {
$documentReferer = "-";
} else {
$documentReferer = urldecode($documentReferer);
}
$documentPath = $_GET["utmp"];
if (empty($documentPath)) {
$documentPath = "";
} else {
$documentPath = urldecode($documentPath);
}
$account = $_GET["utmac"];
$userAgent = $_SERVER["HTTP_USER_AGENT"];
if (empty($userAgent)) {
$userAgent = "";
}
// Try and get visitor cookie from the request.
$cookie = $_COOKIE[COOKIE_NAME];
$guidHeader = $_SERVER["HTTP_X_DCMGUID"];
if (empty($guidHeader)) {
$guidHeader = $_SERVER["HTTP_X_UP_SUBNO"];
}
if (empty($guidHeader)) {
$guidHeader = $_SERVER["HTTP_X_JPHONE_UID"];
}
if (empty($guidHeader)) {
$guidHeader = $_SERVER["HTTP_X_EM_UID"];
}
$visitorId = getVisitorId($guidHeader, $account, $userAgent, $cookie);
// Always try and add the cookie to the response.
setrawcookie(
COOKIE_NAME,
$visitorId,
$timeStamp + COOKIE_USER_PERSISTENCE,
COOKIE_PATH);
$utmGifLocation = "http://www.google-analytics.com/__utm.gif";
// Construct the gif hit url.
$utmUrl = $utmGifLocation . "?" .
"utmwv=" . VERSION .
"&utmn=" . getRandomNumber() .
"&utmhn=" . urlencode($domainName) .
"&utmr=" . urlencode($documentReferer) .
"&utmp=" . urlencode($documentPath) .
"&utmac=" . $account .
"&utmcc=__utma%3D999.999.999.999.999.1%3B" .
"&utmvid=" . $visitorId .
"&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);
sendRequestToGoogleAnalytics($utmUrl);
// If the debug parameter is on, add a header to the response that contains
// the url that was used to contact Google Analytics.
if (!empty($_GET["utmdebug"])) {
header("X-GA-MOBILE-URL:" . $utmUrl);
}
// Finally write the gif data to the response.
writeGifData();
}
?><?php
trackPageView();
?>
- php 移动网站添加 google Analytics统计代码
- Google Analytics电子商务代码添加
- google analytics 添加跟踪代码
- 使用Google Analytics来统计网站流量
- Java调用Google Analytics API实现网站统计
- 如何利用google analytics事件目标统计网站转化率
- Java调用Google Analytics API实现网站统计
- 怎样在网站中添加google统计代码
- 怎样在网站中添加google统计代码
- Google Analytics统计代码GA.JS中文教程
- Google Analytics统计代码GA.JS中文教程
- 网站访问统计系统Piwik安装使用:支持插件扩展统计媲美Google Analytics
- [cnblogs镜像]博客园cnblogs添加访问统计:百度统计与Google Analytics 详细教程
- Microsoft adCenter Analytics统计与Google Analytics、雅虎统计对比
- google analytics--网站分析利器
- 使用Google Analytics分析网站
- 使用Google Analytics分析网站
- 没有网站也能玩转Google Analytics
- 各种模型的比较 ------ Model-View-Control(MVC)架构(2)
- 5.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
- android.preference
- 使用Apache的模块:rewrite模块语法格式
- tcp/ip协议详解
- php 移动网站添加 google Analytics统计代码
- TCP/IP详解之IP分片
- android 常见死机问题--log分析
- java网络编程实例1——用socket类写http客户端
- opencv编译错误(PatchGenerator)
- Android之使用Android-query框架进行开发
- CSS初步理解
- POJ 2262
- VAOs, VBOs, Vertex and Fragment Shaders