Class: TrafficSource
- Inherits:
-
Object
- Object
- TrafficSource
- Defined in:
- lib/visitor_sources/traffic_source.rb
Constant Summary collapse
- COOKIE_LINE_PARAMETERS =
['encoder_version', 'unix_timestamp', 'medium', 'term', 'source', 'campaign', 'content']
- STANDARD_PARAMETER_MAPPING =
{:medium => :utm_medium, :term => :utm_term, :source => :utm_source, :campaign => :utm_campaign, :content => :utm_content}
Instance Attribute Summary collapse
-
#campaign ⇒ Object
Returns the value of attribute campaign.
-
#content ⇒ Object
Returns the value of attribute content.
-
#custom_parameter_mapping ⇒ Object
Returns the value of attribute custom_parameter_mapping.
-
#encoder_version ⇒ Object
Returns the value of attribute encoder_version.
-
#env ⇒ Object
Returns the value of attribute env.
-
#medium ⇒ Object
Returns the value of attribute medium.
-
#source ⇒ Object
Returns the value of attribute source.
-
#term ⇒ Object
Returns the value of attribute term.
-
#unix_timestamp ⇒ Object
Returns the value of attribute unix_timestamp.
Class Method Summary collapse
- .initialize_from_string(string) ⇒ Object
- .initialize_with_rack_env(env, custom_parameter_mapping = {}) ⇒ Object
- .updated_rack_environment(old_env, custom_parameter_mapping = {}, ignore_duplicate_source_within = 120) ⇒ Object
Instance Method Summary collapse
- #date ⇒ Object
-
#initialize(attributes = {}) ⇒ TrafficSource
constructor
A new instance of TrafficSource.
- #query_string_value_for(attribute) ⇒ Object
- #same_as?(traffic_source) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ TrafficSource
Returns a new instance of TrafficSource.
23 24 25 26 27 |
# File 'lib/visitor_sources/traffic_source.rb', line 23 def initialize(attributes={}) attributes.each do |key, value| self.send("#{key}=", value) end end |
Instance Attribute Details
#campaign ⇒ Object
Returns the value of attribute campaign.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def campaign @campaign end |
#content ⇒ Object
Returns the value of attribute content.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def content @content end |
#custom_parameter_mapping ⇒ Object
Returns the value of attribute custom_parameter_mapping.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def custom_parameter_mapping @custom_parameter_mapping end |
#encoder_version ⇒ Object
Returns the value of attribute encoder_version.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def encoder_version @encoder_version end |
#env ⇒ Object
Returns the value of attribute env.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def env @env end |
#medium ⇒ Object
Returns the value of attribute medium.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def medium @medium end |
#source ⇒ Object
Returns the value of attribute source.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def source @source end |
#term ⇒ Object
Returns the value of attribute term.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def term @term end |
#unix_timestamp ⇒ Object
Returns the value of attribute unix_timestamp.
17 18 19 |
# File 'lib/visitor_sources/traffic_source.rb', line 17 def @unix_timestamp end |
Class Method Details
.initialize_from_string(string) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/visitor_sources/traffic_source.rb', line 76 def TrafficSource.initialize_from_string(string) string_attributes = string.split("|") traffic_source = TrafficSource.new(:encoder_version => string_attributes[0].to_i, :unix_timestamp => string_attributes[1].to_i) COOKIE_LINE_PARAMETERS.last(5).each_with_index do |attribute, index| traffic_source.send("#{attribute}=", string_attributes[index+2]) end return traffic_source end |
.initialize_with_rack_env(env, custom_parameter_mapping = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/visitor_sources/traffic_source.rb', line 42 def TrafficSource.initialize_with_rack_env(env, custom_parameter_mapping = {}) traffic_source = self.new(:env => env, :custom_parameter_mapping => custom_parameter_mapping, :unix_timestamp => Time.now.to_i, :encoder_version => 1) COOKIE_LINE_PARAMETERS.last(5).each do |attribute| traffic_source.send("#{attribute}=", traffic_source.query_string_value_for(attribute.to_sym)) end #special case for adwords auto tagging traffic_source.medium = 'cpc' if traffic_source.medium.nil? && !traffic_source.env["rack.request.query_hash"]["gclid"].nil? if traffic_source.medium.nil? if env["HTTP_REFERER"].nil? traffic_source.medium = "direct" else traffic_source.medium = "referal" unless env["HTTP_REFERER"] =~ /#{env['HTTP_HOST']}/ end end if traffic_source.source.nil? begin uri = URI.parse(env["HTTP_REFERER"]) traffic_source.source = uri.host search_engine = SearchEngine.find(traffic_source.source) if search_engine traffic_source.source = search_engine.name traffic_source.term = Rack::Utils.parse_query(uri.query)[search_engine.search_parameter] if traffic_source.term.nil? traffic_source.medium = "organic" unless traffic_source.medium == 'cpc' end traffic_source.content = uri.path if traffic_source.content.nil? && !search_engine rescue; end end return traffic_source end |
.updated_rack_environment(old_env, custom_parameter_mapping = {}, ignore_duplicate_source_within = 120) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/visitor_sources/traffic_source.rb', line 29 def self.updated_rack_environment(old_env, custom_parameter_mapping = {}, ignore_duplicate_source_within=120) old_env["rack.session"][:traffic_sources] ||= '' traffic_sources = TrafficSources.new(old_env["rack.session"][:traffic_sources]) latest_source = TrafficSource.initialize_with_rack_env(old_env, custom_parameter_mapping) return old_env if latest_source.to_s.nil? if traffic_sources.length > 0 return old_env if latest_source.same_as?(traffic_sources.last) && latest_source.-traffic_sources.last. < ignore_duplicate_source_within end traffic_sources << latest_source old_env["rack.session"][:traffic_sources] = traffic_sources.to_s return old_env end |
Instance Method Details
#date ⇒ Object
107 108 109 |
# File 'lib/visitor_sources/traffic_source.rb', line 107 def date Time.at() end |
#query_string_value_for(attribute) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/visitor_sources/traffic_source.rb', line 91 def query_string_value_for(attribute) if custom_parameter_mapping[attribute] && !env["rack.request.query_hash"][custom_parameter_mapping[attribute].to_s].nil? return env["rack.request.query_hash"][custom_parameter_mapping[attribute].to_s] end if env["rack.request.query_hash"][STANDARD_PARAMETER_MAPPING[attribute].to_s] return env["rack.request.query_hash"][STANDARD_PARAMETER_MAPPING[attribute].to_s] end end |
#same_as?(traffic_source) ⇒ Boolean
100 101 102 103 104 105 |
# File 'lib/visitor_sources/traffic_source.rb', line 100 def same_as?(traffic_source) COOKIE_LINE_PARAMETERS.last(5).each do |attribute| return false if self.send(attribute).to_s != traffic_source.send(attribute).to_s end return true end |
#to_s ⇒ Object
85 86 87 88 89 |
# File 'lib/visitor_sources/traffic_source.rb', line 85 def to_s return nil if medium.nil? # join each element by pipes(|), remove any unnecessary unused pipes from the end COOKIE_LINE_PARAMETERS.collect{|param| self.send(param)}.join("|").gsub(/\|+$/, '') end |