rack-utm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rack-utm.rb +120 -0
  3. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04985930656d4510deea4ea24cd7da8ce4da5a77
4
- data.tar.gz: 22f89051a7956275054f544c5e202cf4678e8cdf
3
+ metadata.gz: 89224a6aa86ece24721e51069f9fb53dd1d104e1
4
+ data.tar.gz: d8ac445757f096d29767cda034b3ef87705a1ca6
5
5
  SHA512:
6
- metadata.gz: 723146a7b42519a318756043f0cf7fc1d40c6dc128e606e92c0f9938029de386874a8e2f8958ec9552976c9befb358d6ce142cadeebbba088d8ce897d5d1c25b
7
- data.tar.gz: be5e4341e83cbd597274afbc870e059d98c7632e5ec2b472bf26be08dd6cd56e81b4ca96f3f547cb0f3b216e04b74ea8abefe51a00bd250f85526892f3708706
6
+ metadata.gz: 8668179892c508a38eb78df21b07f3ca75697cc37c107eb17bd5007e0b324e00d97bf16da37b70ac31359c0ac81c43e184b1d3ea8d23b98b0b2934ea5eb8a1a3
7
+ data.tar.gz: 1caf3c955779c74a406fd98df163c280cc6d57180241df17633ee0a29725ac676726539debef2a4e4b94fdd18ab7b9f3abe190aa73f8abffcd7dfacb76678ea6
@@ -0,0 +1,120 @@
1
+ module Rack
2
+ #
3
+ # Rack Middleware for extracting information from the request params and cookies.
4
+ # It populates +env['affiliate.tag']+, # +env['affiliate.from']+ and
5
+ # +env['affiliate.time'] if it detects a request came from an affiliated link
6
+ #
7
+ class Utm
8
+
9
+ COOKIE_SOURCE = "u_source"
10
+ COOKIE_MEDIUM = "u_medium"
11
+ COOKIE_TERM = "u_term"
12
+ COOKIE_CONTENT = "u_content"
13
+ COOKIE_CAMPAIGN = "u_campaign"
14
+
15
+ COOKIE_FROM = "u_from"
16
+ COOKIE_TIME = "u_time"
17
+ COOKIE_LP = "u_lp"
18
+
19
+ def initialize(app, opts = {})
20
+ @app = app
21
+ @key_param = "utm_source"
22
+ @cookie_ttl = opts[:ttl] || 60*60*24*30 # 30 days
23
+ @cookie_domain = opts[:domain] || nil
24
+ @allow_overwrite = opts[:overwrite].nil? ? true : opts[:overwrite]
25
+ end
26
+
27
+ def call(env)
28
+ req = Rack::Request.new(env)
29
+
30
+ params_tag = req.params[@key_param]
31
+ cookie_tag = req.cookies[COOKIE_SOURCE]
32
+
33
+ if cookie_tag
34
+ source, medium, term, content, campaign, from, time, lp = cookie_info(req)
35
+ end
36
+
37
+ if params_tag && params_tag != cookie_tag
38
+ if source
39
+ if @allow_overwrite
40
+ source, medium, term, content, campaign, from, time, lp = params_info(req)
41
+ end
42
+ else
43
+ source, medium, term, content, campaign, from, time, lp = params_info(req)
44
+ end
45
+ end
46
+
47
+ if source
48
+ env["utm.source"] = source
49
+ env['utm.medium'] = medium
50
+ env['utm.term'] = term
51
+ env['utm.content'] = content
52
+ env['utm.campaign'] = campaign
53
+
54
+ env['utm.from'] = from
55
+ env['utm.time'] = time
56
+ env['utm.lp'] = lp
57
+ end
58
+
59
+ status, headers, body = @app.call(env)
60
+
61
+ if source != cookie_tag
62
+ bake_cookies(headers, source, medium, term, content, campaign, from, time, lp)
63
+ end
64
+
65
+ [status, headers, body]
66
+ end
67
+
68
+ def utm_info(req)
69
+ params_info(req) || cookie_info(req)
70
+ end
71
+
72
+ def params_info(req)
73
+ [
74
+ req.params["utm_source"],
75
+ req.params["utm_medium"],
76
+ req.params["utm_term"],
77
+ req.params["utm_content"],
78
+ req.params["utm_campaign"],
79
+ req.env["HTTP_REFERER"],
80
+ Time.now.to_i,
81
+ req.path
82
+ ]
83
+ end
84
+
85
+ def cookie_info(req)
86
+ [
87
+ req.cookies[COOKIE_SOURCE],
88
+ req.cookies[COOKIE_MEDIUM],
89
+ req.cookies[COOKIE_TERM],
90
+ req.cookies[COOKIE_CONTENT],
91
+ req.cookies[COOKIE_CAMPAIGN],
92
+
93
+ req.cookies[COOKIE_FROM],
94
+ req.cookies[COOKIE_TIME],
95
+ req.cookies[COOKIE_LP]
96
+
97
+ ]
98
+ end
99
+
100
+ protected
101
+ def bake_cookies(headers, source, medium, term, content, campaign, from, time, lp)
102
+ expires = Time.now + @cookie_ttl
103
+ { COOKIE_SOURCE => source,
104
+ COOKIE_MEDIUM => medium,
105
+ COOKIE_TERM => term,
106
+ COOKIE_CONTENT => content,
107
+ COOKIE_CAMPAIGN => campaign,
108
+ COOKIE_FROM => from,
109
+ COOKIE_TIME => time,
110
+ COOKIE_LP => lp
111
+ }.each do |key, value|
112
+ cookie_hash = {:value => value,
113
+ :expires => expires,
114
+ :path => "/"}
115
+ cookie_hash[:domain] = @cookie_domain if @cookie_domain
116
+ Rack::Utils.set_cookie_header!(headers, key, cookie_hash)
117
+ end
118
+ end
119
+ end
120
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-utm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Severin Ulrich
@@ -119,6 +119,7 @@ extra_rdoc_files:
119
119
  files:
120
120
  - LICENSE.txt
121
121
  - README.md
122
+ - lib/rack-utm.rb
122
123
  homepage: http://github.com/silvermind/rack-utm
123
124
  licenses:
124
125
  - MIT