more templates back into cli

This commit is contained in:
Thomas Reynolds 2014-05-26 18:44:11 -07:00
parent be5fad55ca
commit ba01a0a72b
121 changed files with 5 additions and 42 deletions

View file

@ -0,0 +1,31 @@
<%@ Page Language="C#" %>
<script language="C#" runat="server">
// Copyright 2009 Google Inc. All Rights Reserved.
private const string GaAccount = "ACCOUNT ID GOES HERE";
private const string GaPixel = "ga.aspx";
private string GoogleAnalyticsGetImageUrl() {
System.Text.StringBuilder url = new System.Text.StringBuilder();
url.Append(GaPixel + "?");
url.Append("utmac=").Append(GaAccount);
Random RandomClass = new Random();
url.Append("&utmn=").Append(RandomClass.Next(0x7fffffff));
string referer = "-";
if (Request.UrlReferrer != null
&& "" != Request.UrlReferrer.ToString()) {
referer = Request.UrlReferrer.ToString();
}
url.Append("&utmr=").Append(HttpUtility.UrlEncode(referer));
if (HttpContext.Current.Request.Url != null) {
url.Append("&utmp=").Append(HttpUtility.UrlEncode(Request.Url.PathAndQuery));
}
url.Append("&guid=ON");
return url.ToString();
}
</script>

View file

@ -0,0 +1,2 @@
<% string googleAnalyticsImageUrl = GoogleAnalyticsGetImageUrl(); %>
<img src="<%= googleAnalyticsImageUrl %>" />

View file

@ -0,0 +1,195 @@
<% @Page Language="C#" ContentType="image/gif"%><%
@Import Namespace="System.Net" %><%
@Import Namespace="System.Security.Cryptography" %><%
@Import Namespace="System.Text" %><script runat="server" language="c#">
/**
Copyright 2009 Google Inc. All Rights Reserved.
**/
// Tracker version.
private const string Version = "4.4sa";
private const string CookieName = "__utmmobile";
// The path the cookie will be available to, edit this to use a different
// cookie path.
private const string CookiePath = "/";
// Two years in seconds.
private readonly TimeSpan CookieUserPersistence = TimeSpan.FromSeconds(63072000);
// 1x1 transparent GIF
private readonly byte[] GifData = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b
};
private static readonly Regex IpAddressMatcher =
new Regex(@"^([^.]+\.[^.]+\.[^.]+\.).*");
// A string is empty in our terms, if it is null, empty or a dash.
private static bool IsEmpty(string input) {
return input == null || "-" == input || "" == input;
}
// The last octect of the IP address is removed to anonymize the user.
private static string GetIP(string remoteAddress) {
if (IsEmpty(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
Match m = IpAddressMatcher.Match(remoteAddress);
if (m.Success) {
return m.Groups[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.
private static string GetVisitorId(
string guid, string account, string userAgent, HttpCookie cookie) {
// If there is a value in the cookie, don't change it.
if (cookie != null && cookie.Value != null) {
return cookie.Value;
}
String message;
if (!IsEmpty(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 + GetRandomNumber() + Guid.NewGuid().ToString();
}
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
byte[] sum = md5.ComputeHash(messageBytes);
string md5String = BitConverter.ToString(sum);
md5String = md5String.Replace("-","");
md5String = md5String.PadLeft(32, '0');
return "0x" + md5String.Substring(0, 16);
}
// Get a random number string.
private static String GetRandomNumber() {
Random RandomClass = new Random();
return RandomClass.Next(0x7fffffff).ToString();
}
// Writes the bytes of a 1x1 transparent gif into the response.
private void WriteGifData() {
Response.AddHeader(
"Cache-Control",
"private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Expires", "Wed, 17 Sep 1975 21:32:10 GMT");
Response.Buffer = false;
Response.OutputStream.Write(GifData, 0, GifData.Length);
}
// 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.
private void SendRequestToGoogleAnalytics(string utmUrl) {
try {
WebRequest connection = WebRequest.Create(utmUrl);
((HttpWebRequest)connection).UserAgent = Request.UserAgent;
connection.Headers.Add("Accepts-Language",
Request.Headers.Get("Accepts-Language"));
using (WebResponse resp = connection.GetResponse()) {
// Ignore response
}
} catch (Exception ex) {
if (Request.QueryString.Get("utmdebug") != null) {
throw new Exception("Error contacting Google Analytics", ex);
}
}
}
// 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.
private void TrackPageView() {
TimeSpan timeSpan = (DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime());
string timeStamp = timeSpan.TotalSeconds.ToString();
string domainName = Request.ServerVariables["SERVER_NAME"];
if (IsEmpty(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.
string documentReferer = Request.QueryString.Get("utmr");
if (IsEmpty(documentReferer)) {
documentReferer = "-";
} else {
documentReferer = HttpUtility.UrlDecode(documentReferer);
}
string documentPath = Request.QueryString.Get("utmp");
if (IsEmpty(documentPath)) {
documentPath = "";
} else {
documentPath = HttpUtility.UrlDecode(documentPath);
}
string account = Request.QueryString.Get("utmac");
string userAgent = Request.UserAgent;
if (IsEmpty(userAgent)) {
userAgent = "";
}
// Try and get visitor cookie from the request.
HttpCookie cookie = Request.Cookies.Get(CookieName);
string visitorId = GetVisitorId(
Request.Headers.Get("X-DCMGUID"), account, userAgent, cookie);
// Always try and add the cookie to the response.
HttpCookie newCookie = new HttpCookie(CookieName);
newCookie.Value = visitorId;
newCookie.Expires = DateTime.Now + CookieUserPersistence;
newCookie.Path = CookiePath;
Response.Cookies.Add(newCookie);
string utmGifLocation = "http://www.google-analytics.com/__utm.gif";
// Construct the gif hit url.
string utmUrl = utmGifLocation + "?" +
"utmwv=" + Version +
"&utmn=" + GetRandomNumber() +
"&utmhn=" + HttpUtility.UrlEncode(domainName) +
"&utmr=" + HttpUtility.UrlEncode(documentReferer) +
"&utmp=" + HttpUtility.UrlEncode(documentPath) +
"&utmac=" + account +
"&utmcc=__utma%3D999.999.999.999.999.1%3B" +
"&utmvid=" + visitorId +
"&utmip=" + GetIP(Request.ServerVariables["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 (Request.QueryString.Get("utmdebug") != null) {
Response.AddHeader("X-GA-MOBILE-URL", utmUrl);
}
// Finally write the gif data to the response.
WriteGifData();
}
</script><% TrackPageView(); %>

View file

@ -0,0 +1,44 @@
<%@ Page Language="C#" %>
<script language="C#" runat="server">
private const string GaAccount = "MO-3845491-5";
private const string GaPixel = "ga.aspx";
private string GoogleAnalyticsGetImageUrl() {
System.Text.StringBuilder url = new System.Text.StringBuilder();
url.Append(GaPixel + "?");
url.Append("utmac=").Append(GaAccount);
Random RandomClass = new Random();
url.Append("&utmn=").Append(RandomClass.Next(0x7fffffff));
string referer = "-";
if (Request.UrlReferrer != null
&& "" != Request.UrlReferrer.ToString()) {
referer = Request.UrlReferrer.ToString();
}
url.Append("&utmr=").Append(HttpUtility.UrlEncode(referer));
if (HttpContext.Current.Request.Url != null) {
url.Append("&utmp=").Append(HttpUtility.UrlEncode(Request.Url.PathAndQuery));
}
url.Append("&guid=ON");
return url.ToString();
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample Mobile Analytics Page</title>
</head>
<body>
Publisher's content here.
<%
string googleAnalyticsImageUrl = GoogleAnalyticsGetImageUrl();
%>
<img src="<%= googleAnalyticsImageUrl %>" />
Testing: <%= googleAnalyticsImageUrl %>
</body>
</html>

View file

@ -0,0 +1,225 @@
<%@ page language="java"
contentType="image/gif"
import="java.io.ByteArrayOutputStream,
java.io.IOException,
java.io.OutputStream,
java.io.UnsupportedEncodingException,
java.math.BigInteger,
java.net.HttpURLConnection,
java.net.URLConnection,
java.net.URL,
java.net.URLEncoder,
java.net.URLDecoder,
java.security.MessageDigest,
java.security.NoSuchAlgorithmException,
javax.servlet.http.Cookie,
java.util.regex.Pattern,
java.util.regex.Matcher,
java.util.UUID" %><%!
/**
Copyright 2009 Google Inc. All Rights Reserved.
**/
// Tracker version.
private static final String version = "4.4sj";
private static final String COOKIE_NAME = "__utmmobile";
// The path the cookie will be available to, edit this to use a different
// cookie path.
private static final String COOKIE_PATH = "/";
// Two years in seconds.
private static final int COOKIE_USER_PERSISTENCE = 63072000;
// 1x1 transparent GIF
private static final byte[] GIF_DATA = new byte[] {
(byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61,
(byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x80, (byte)0xff,
(byte)0x00, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x2c, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x02,
(byte)0x02, (byte)0x44, (byte)0x01, (byte)0x00, (byte)0x3b
};
// A string is empty in our terms, if it is null, empty or a dash.
private static boolean isEmpty(String in) {
return in == null || "-".equals(in) || "".equals(in);
}
// The last octect of the IP address is removed to anonymize the user.
private static String getIP(String remoteAddress) {
if (isEmpty(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
String regex = "^([^.]+\\.[^.]+\\.[^.]+\\.).*";
Pattern getFirstBitOfIPAddress = Pattern.compile(regex);
Matcher m = getFirstBitOfIPAddress.matcher(remoteAddress);
if (m.matches()) {
return m.group(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.
private static String getVisitorId(
String guid, String account, String userAgent, Cookie cookie)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
// If there is a value in the cookie, don't change it.
if (cookie != null && cookie.getValue() != null) {
return cookie.getValue();
}
String message;
if (!isEmpty(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 + getRandomNumber() + UUID.randomUUID().toString();
}
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(message.getBytes("UTF-8"), 0, message.length());
byte[] sum = m.digest();
BigInteger messageAsNumber = new BigInteger(1, sum);
String md5String = messageAsNumber.toString(16);
// Pad to make sure id is 32 characters long.
while (md5String.length() < 32) {
md5String = "0" + md5String;
}
return "0x" + md5String.substring(0, 16);
}
// Get a random number string.
private static String getRandomNumber() {
return Integer.toString((int) (Math.random() * 0x7fffffff));
}
// Writes the bytes of a 1x1 transparent gif into the response.
private void writeGifData(HttpServletResponse response) throws IOException {
response.addHeader(
"Cache-Control",
"private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
response.addHeader("Pragma", "no-cache");
response.addHeader("Expires", "Wed, 17 Sep 1975 21:32:10 GMT");
ServletOutputStream output = response.getOutputStream();
output.write(GIF_DATA);
output.flush();
}
// 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.
private void sendRequestToGoogleAnalytics(
String utmUrl, HttpServletRequest request) throws Exception {
try {
URL url = new URL(utmUrl);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
connection.addRequestProperty("User-Agent",
request.getHeader("User-Agent"));
connection.addRequestProperty("Accepts-Language",
request.getHeader("Accepts-Language"));
connection.getContent();
} catch (Exception e) {
if (request.getParameter("utmdebug") != null) {
throw new Exception(e);
}
}
}
// 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.
private void trackPageView(
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String timeStamp = Long.toString(System.currentTimeMillis() / 1000);
String domainName = request.getServerName();
if (isEmpty(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.
String documentReferer = request.getParameter("utmr");
if (isEmpty(documentReferer)) {
documentReferer = "-";
} else {
documentReferer = URLDecoder.decode(documentReferer, "UTF-8");
}
String documentPath = request.getParameter("utmp");
if (isEmpty(documentPath)) {
documentPath = "";
} else {
documentPath = URLDecoder.decode(documentPath, "UTF-8");
}
String account = request.getParameter("utmac");
String userAgent = request.getHeader("User-Agent");
if (isEmpty(userAgent)) {
userAgent = "";
}
// Try and get visitor cookie from the request.
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
if (cookies != null) {
for(int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(COOKIE_NAME)) {
cookie = cookies[i];
}
}
}
String visitorId = getVisitorId(
request.getHeader("X-DCMGUID"), account, userAgent, cookie);
// Always try and add the cookie to the response.
Cookie newCookie = new Cookie(COOKIE_NAME, visitorId);
newCookie.setMaxAge(COOKIE_USER_PERSISTENCE);
newCookie.setPath(COOKIE_PATH);
response.addCookie(newCookie);
String utmGifLocation = "http://www.google-analytics.com/__utm.gif";
// Construct the gif hit url.
String utmUrl = utmGifLocation + "?" +
"utmwv=" + version +
"&utmn=" + getRandomNumber() +
"&utmhn=" + URLEncoder.encode(domainName, "UTF-8") +
"&utmr=" + URLEncoder.encode(documentReferer, "UTF-8") +
"&utmp=" + URLEncoder.encode(documentPath, "UTF-8") +
"&utmac=" + account +
"&utmcc=__utma%3D999.999.999.999.999.1%3B" +
"&utmvid=" + visitorId +
"&utmip=" + getIP(request.getRemoteAddr());
sendRequestToGoogleAnalytics(utmUrl, request);
// If the debug parameter is on, add a header to the response that contains
// the url that was used to contact Google Analytics.
if (request.getParameter("utmdebug") != null) {
response.setHeader("X-GA-MOBILE-URL", utmUrl);
}
// Finally write the gif data to the response.
writeGifData(response);
}
%><%
// Let exceptions bubble up to container, for better debugging.
trackPageView(request, response);
%>

View file

@ -0,0 +1,35 @@
<%@ page import="java.io.UnsupportedEncodingException,
java.net.URLEncoder" %>
<%!
// Copyright 2009 Google Inc. All Rights Reserved.
private static final String GA_ACCOUNT = "ACCOUNT ID GOES HERE";
private static final String GA_PIXEL = "ga.jsp";
private String googleAnalyticsGetImageUrl(
HttpServletRequest request) throws UnsupportedEncodingException {
StringBuilder url = new StringBuilder();
url.append(GA_PIXEL + "?");
url.append("utmac=").append(GA_ACCOUNT);
url.append("&utmn=").append(Integer.toString((int) (Math.random() * 0x7fffffff)));
String referer = request.getHeader("referer");
String query = request.getQueryString();
String path = request.getRequestURI();
if (referer == null || "".equals(referer)) {
referer = "-";
}
url.append("&utmr=").append(URLEncoder.encode(referer, "UTF-8"));
if (path != null) {
if (query != null) {
path += "?" + query;
}
url.append("&utmp=").append(URLEncoder.encode(path, "UTF-8"));
}
url.append("&guid=ON");
return url.toString();
}
%>

View file

@ -0,0 +1,2 @@
<% String googleAnalyticsImageUrl = googleAnalyticsGetImageUrl(request); %>
<img src="<%= googleAnalyticsImageUrl %>" />

View file

@ -0,0 +1,51 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.UnsupportedEncodingException,
java.net.URLEncoder" %>
<%!
private static final String GA_ACCOUNT = "MO-3845491-5";
private static final String GA_PIXEL = "ga.jsp";
private String googleAnalyticsGetImageUrl(
HttpServletRequest request) throws UnsupportedEncodingException {
StringBuilder url = new StringBuilder();
url.append(GA_PIXEL + "?");
url.append("utmac=").append(GA_ACCOUNT);
url.append("&utmn=").append(Integer.toString((int) (Math.random() * 0x7fffffff)));
String referer = request.getHeader("referer");
String query = request.getQueryString();
String path = request.getRequestURI();
if (referer == null || "".equals(referer)) {
referer = "-";
}
url.append("&utmr=").append(URLEncoder.encode(referer, "UTF-8"));
if (path != null) {
if (query != null) {
path += "?" + query;
}
url.append("&utmp=").append(URLEncoder.encode(path, "UTF-8"));
}
url.append("&guid=ON");
return url.toString();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample Mobile Analytics Page</title>
</head>
<body>
Publishers content here.
<%
String googleAnalyticsImageUrl = googleAnalyticsGetImageUrl(request);
%>
<img src="<%= googleAnalyticsImageUrl %>" />
Testing: <%= googleAnalyticsImageUrl %>
</body>
</html>

View file

@ -0,0 +1,176 @@
<?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];
$visitorId = getVisitorId(
$_SERVER["HTTP_X_DCMGUID"], $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();
?>

View file

@ -0,0 +1,30 @@
<?php
// Copyright 2009 Google Inc. All Rights Reserved.
$GA_ACCOUNT = "ACCOUNT ID GOES HERE";
$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 (empty($referer)) {
$referer = "-";
}
$url .= "&utmr=" . urlencode($referer);
if (!empty($path)) {
$url .= "&utmp=" . urlencode($path);
}
$url .= "&guid=ON";
return $url;
}
?>

View file

@ -0,0 +1,4 @@
<?php
$googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
?>
<img src="<?= $googleAnalyticsImageUrl ?>" />

View file

@ -0,0 +1,44 @@
<?php
$GA_ACCOUNT = "MO-3845491-5";
$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 (empty($referer)) {
$referer = "-";
}
$url .= "&utmr=" . urlencode($referer);
if (!empty($path)) {
$url .= "&utmp=" . urlencode($path);
}
$url .= "&guid=ON";
return $url;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample Mobile Analytics Page</title>
</head>
<body>
Publishers content here.
<?php
$googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
?>
<img src="<?= $googleAnalyticsImageUrl ?>" />
Testing: <?= $googleAnalyticsImageUrl ?>
</body>
</html>

View file

@ -0,0 +1,195 @@
#!/usr/bin/perl -w
#
# Copyright 2009 Google Inc. All Rights Reserved.
use CGI;
use Digest::MD5 qw(md5_hex);
use LWP::UserAgent;
use URI::Escape;
use strict;
# Tracker version.
use constant VERSION => '4.4sp';
use constant COOKIE_NAME => '__utmmobile';
# The path the cookie will be available to, edit this to use a different
# cookie path.
use constant COOKIE_PATH => '/';
# Two years.
use constant COOKIE_USER_PERSISTENCE => '+2y';
# 1x1 transparent GIF
my @GIF_DATA = (
0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b);
my $query = new CGI;
# The last octect of the IP address is removed to anonymize the user.
sub get_ip {
my ($remote_address) = @_;
if ($remote_address eq "") {
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
if ($remote_address =~ /^((\d{1,3}\.){3})\d{1,3}$/) {
return $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.
sub get_visitor_id {
my ($guid, $account, $user_agent, $cookie) = @_;
# If there is a value in the cookie, don't change it.
if ($cookie ne "") {
return $cookie;
}
my $message = "";
if ($guid ne "") {
# Create the visitor id using the guid.
$message = $guid . $account;
} else {
# otherwise this is a new user, create a new random id.
$message = $user_agent . get_random_number();
}
my $md5_string = md5_hex($message);
return "0x" . substr($md5_string, 0, 16);
}
# Get a random number string.
sub get_random_number {
return int(rand(0x7fffffff));
}
# Writes the bytes of a 1x1 transparent gif into the response.
sub write_gif_data {
my ($cookie, $utm_url) = @_;
my @header_args = (
-type => 'image/gif',
-Cache_Control =>
'private, no-cache, no-cache=Set-Cookie, proxy-revalidate',
-Pragma => 'no-cache',
-cookie => $cookie,
-expires => '-1d');
# If the debug parameter is on, add a header to the response that contains
# the url that was used to contact Google Analytics.
if (defined($query->param('utmdebug'))) {
push(@header_args, -X_GA_MOBILE_URL => $utm_url);
}
print $query->header(@header_args);
print pack("C35", @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.
sub send_request_to_google_analytics {
my ($utm_url) = @_;
my $ua = LWP::UserAgent->new;
if (exists($ENV{'HTTP_ACCEPT_LANGUAGE'})) {
$ua->default_header('Accepts-Language' => $ENV{'HTTP_ACCEPT_LANGUAGE'});
}
if (exists($ENV{'HTTP_USER_AGENT'})) {
$ua->agent($ENV{'HTTP_USER_AGENT'});
}
my $ga_output = $ua->get($utm_url);
if (defined($query->param('utmdebug')) && !$ga_output->is_success) {
print $ga_output->status_line;
}
}
# 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.
sub track_page_view {
my $domain_name = "";
if (exists($ENV{'SERVER_NAME'})) {
$domain_name = $ENV{'SERVER_NAME'};
}
# 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.
my $document_referer = "-";
if (defined($query->param('utmr'))) {
$document_referer = uri_unescape($query->param('utmr'));
}
my $document_path = "";
if (defined($query->param('utmp'))) {
$document_path = uri_unescape($query->param('utmp'));
}
my $account = $query->param('utmac');
my $user_agent = "";
if (exists($ENV{'HTTP_USER_AGENT'})) {
$user_agent = $ENV{'HTTP_USER_AGENT'};
}
# Try and get visitor cookie from the request.
my $cookie = "";
if (defined($query->cookie(COOKIE_NAME))) {
$cookie = $query->cookie(COOKIE_NAME);
}
my $guid = "";
if (exists($ENV{'HTTP_X_DCMGUID'})) {
$guid = $ENV{'HTTP_X_DCMGUID'};
}
my $visitor_id = get_visitor_id($guid, $account, $user_agent, $cookie);
# Always try and add the cookie to the response.
my $new_cookie = $query->cookie(
-name => COOKIE_NAME,
-value => $visitor_id,
-path => COOKIE_PATH,
-expires => COOKIE_USER_PERSISTENCE);
my $utm_gif_location = "http://www.google-analytics.com/__utm.gif";
my $remote_address = "";
if (exists($ENV{'REMOTE_ADDR'})) {
$remote_address = $ENV{'REMOTE_ADDR'};
}
# Construct the gif hit url.
my $utm_url = $utm_gif_location . '?' .
'utmwv=' . VERSION .
'&utmn=' . get_random_number() .
'&utmhn=' . uri_escape($domain_name) .
'&utmr=' . uri_escape($document_referer) .
'&utmp=' . uri_escape($document_path) .
'&utmac=' . $account .
'&utmcc=__utma%3D999.999.999.999.999.1%3B' .
'&utmvid=' . $visitor_id .
'&utmip=' . get_ip($remote_address);
send_request_to_google_analytics($utm_url);
# Finally write the gif data to the response.
write_gif_data($new_cookie, $utm_url);
}
track_page_view();

View file

@ -0,0 +1,27 @@
# Copyright 2009 Google Inc. All Rights Reserved.
use URI::Escape;
use constant GA_ACCOUNT => 'ACCOUNT ID GOES HERE';
use constant GA_PIXEL => 'ga.pl';
sub google_analytics_get_image_url {
my $url = '';
$url .= GA_PIXEL . '?';
$url .= 'utmac=' . GA_ACCOUNT;
$url .= '&utmn=' . int(rand(0x7fffffff));
my $referer = $ENV{'HTTP_REFERER'};
my $query = $ENV{'QUERY_STRING'};
my $path = $ENV{'REQUEST_URI'};
if ($referer eq "") {
$referer = '-';
}
$url .= '&utmr=' . uri_escape($referer);
$url .= '&utmp=' . uri_escape($path);
$url .= '&guid=ON';
$url;
}

View file

@ -0,0 +1 @@
print '<img src="' . google_analytics_get_image_url() . '" />';

View file

@ -0,0 +1,38 @@
#!/usr/bin/perl -w
#
# Copyright 2009 Google Inc. All Rights Reserved.
use strict;
use URI::Escape;
use constant GA_ACCOUNT => 'MO-3845491-5';
use constant GA_PIXEL => 'ga.pl';
sub google_analytics_get_image_url {
my $url = '';
$url .= GA_PIXEL . '?';
$url .= 'utmac=' . GA_ACCOUNT;
$url .= '&utmn=' . int(rand(0x7fffffff));
my $referer = $ENV{'HTTP_REFERER'};
my $query = $ENV{'QUERY_STRING'};
my $path = $ENV{'REQUEST_URI'};
if ($referer eq "") {
$referer = '-';
}
$url .= '&utmr=' . uri_escape($referer);
$url .= '&utmp=' . uri_escape($path);
$url .= '&guid=ON';
$url;
}
print "Content-Type: text/html; charset=ISO-8859-1\n\n";
print '<html><head></head><body>';
print '<img src="' . google_analytics_get_image_url() . '" />';
print google_analytics_get_image_url();
print '</body></html>';
exit (0);