Class: Garu

Inherits:
Object
  • Object
show all
Defined in:
lib/garu.rb

Constant Summary collapse

VERSION =

Tracker version. (Kept from the google tools for mobile

> as it’s using the same parameters)

"4.4sh"
"__utmmobile"
"/"
63072000
UTMGIFLOCATION =

Google URL called to record the tracker hit

"http://www.google-analytics.com/__utm.gif"
GIF_DATA =

1x1 transparent GIF to be sent back in the response body

[
    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.chr
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, request) ⇒ Garu

Returns a new instance of Garu.



39
40
41
42
# File 'lib/garu.rb', line 39

def initialize (, request)
  @request = request
  @account = 
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



13
14
15
# File 'lib/garu.rb', line 13

def 
  @account
end

#requestObject

Returns the value of attribute request.



13
14
15
# File 'lib/garu.rb', line 13

def request
  @request
end

Instance Method Details

#getIP(remoteAddress = nil) ⇒ Object

The last octect of the IP address is removed to

> anonymize the user.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/garu.rb', line 51

def getIP (remoteAddress=nil)
  if remoteAddress.nil?
    remoteAddress = @request.env['REMOTE_ADDR'].split(',').first
  end
  matches = /^([^.]+\.[^.]+\.[^.]+\.).*/.match(remoteAddress)
  if !matches[1].nil?
    remoteAddress = matches[1]+"0"
  else
    remoteAddress = ""
  end
  
  remoteAddress
end

#getRandomNumberObject

Get a random number string.



45
46
47
# File 'lib/garu.rb', line 45

def getRandomNumber
  return rand(0x7fffffff).to_s
end

#getVisitorId(guid, account, userAgent, cookie) ⇒ Object

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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/garu.rb', line 69

def getVisitorId(guid, , userAgent, cookie)
  # If there is a value in the cookie, don't change it.
  if (!cookie.nil?)
    return cookie
  end
  
  message = ""
  
  if (!guid.nil?)
    # Create the visitor id using the guid.
    message = guid + 
  else
    # Otherwise this is a new user, create a new
    # => random id.
    message = userAgent + getRandomNumber
  end
  
  md5String = Digest::MD5.hexdigest(message)
  
  return "0x" + md5String[0..16]
end

#sendRequestToGoogleAnalytics(utmUrl) ⇒ Object

Make a tracking request to Google Analytics

> from this server.

> Copies the headers from the original

> request to the new one.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/garu.rb', line 95

def sendRequestToGoogleAnalytics(utmUrl)
  uri = URI.parse(utmUrl)
  req = Net::HTTP::Get.new(uri.request_uri)
  req['user_agent'] = @request.env["HTTP_USER_AGENT"]
  req['Accepts-Language:'] = @request.env["HTTP_ACCEPT_LANGUAGE"]
  
  res = Net::HTTP.start(uri.host, uri.port) {|http|
    http.request(req)
  }
  res
end

#trackPageViewObject

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.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/garu.rb', line 111

def trackPageView()
  timeStamp = Time.now.getutc.to_i
  domainName = @request.host
  
  if domainName.nil?
    domainName = ""
  end
  
  # 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 = @request.env["HTTP_REFERER"]
  
  if (documentReferer.nil? && documentReferer != "0")
    documentReferer = "-"
  else
    documentReferer = URI.unescape(documentReferer)
  end
  
  documentPath = @request.path
  if documentPath.nil?
    documentPath = ""
  else
    documentPath = URI.unescape(documentPath)
  end
  
   = @account
  userAgent = @request.env["HTTP_USER_AGENT"]
  if userAgent.nil?
    userAgent = ""
  end
  
  # Try and get visitor cookie from the request.
  cookie = @request.cookies[Garu::COOKIE_NAME]
  
  visitorId = getVisitorId(@request.env["HTTP_X_DCMGUID"], , userAgent, cookie)
  
  # Construct the gif hit url.
  utmUrl = Garu::UTMGIFLOCATION + "?" +
      "utmwv=" + Garu::VERSION +
      "&utmn=" + getRandomNumber +
      "&utmhn=" + URI.escape(domainName) +
      "&utmr=" + URI.escape(documentReferer) +
      "&utmp=" + URI.escape(documentPath) +
      "&utmac=" + .to_s +
      "&utmcc=__utma%3D999.999.999.999.999.1%3B" +
      "&utmvid=" + visitorId +
      "&utmip=" + getIP(@request.env["REMOTE_ADDR"])
  
  gaResponse = sendRequestToGoogleAnalytics(utmUrl)
  
  response = {
    'headers' => {
        'Content-Type'  => "image/gif",
        'Cache-Control' => "private, no-cache, no-cache=Set-Cookie, proxy-revalidate",
        'Pragma'        => "no-cache",
        'Expires'       => "Wed, 17 Sep 1975 21:32:10 GMT"
    },
    'body' => GIF_DATA.join
  }
  
  # 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.params["utmdebug"].nil?
    response["headers"].merge!({"X-GA-MOBILE-URL" => utmUrl})
  end
  
  # If no cookie was passed with the request,
  # => a new one has been created and therefore
  # => needs to be sent back in the response from Garu
  if !cookie
    response.merge!({'cookie' => {
      'name'     => COOKIE_NAME,
      'value'    => visitorId,
      'path'     => COOKIE_PATH,
      'domain'   => domainName,
      'expires'  => Time.at(timeStamp + COOKIE_USER_PERSISTENCE)
    }})
  end

  response
end