Sha256: 288936e37e7f0b363eb00dfcfaae612824c7a7e3f4acd39fd1bb616efecea288

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

require 'digest/md5'

# Mirrors a browser cookie installed on the client
#
# == Purpose
#
# A +User+ may have many cookies, and it is through these cookies that associated
# +PageView+ records are tracked.
#
# == Behavior
#
# The cookie is tracked through it's hashed id (hid), and maintains the most 
# recent campaign code passed from the associated client.
#
# Immediately after the cookie is loaded or created, a +PageView+ is generated.
# It is given the current +code+ of the cookie, if it exists, and if either the 
# cookie itself is new, or the code on it is new (has changed), that +PageView+ 
# is marked as a new visit.
#
# In this way, a +PageView+ will be credited to a campaign code even if it a
# code is not passed in its actual request.  The timeline might look like this:
#
# 1. A client visits the site normally with no campaign code.
#   - A cookie is created/installed
#   - A new visit, uncoded page view is created
#
# 2. The client visits another page.
#   - The installed cookie is loaded
#   - An uncoded page view is created.
#
# 3. The same client (pre-existing cookie) visits the site with code ABC.
#   - The installed cookie is loaded, its code updated to ABC.
#   - A new visit, ABC coded page view is created
#
# 4. The client visits another page.
#   - The installed cookie is loaded
#   - An ABC coded page view is created
#
# In this manner, page views are always credited to the most recently loaded
# code, with the assumption that it was the most recently seen code that
# prompted the client to (re)visit the site.
#
class TrackingCookie < ActiveRecord::Base
  belongs_to :user, :inverse_of => :tracking_cookies
  has_many :page_views
  after_save :generate_hid, :on => :create

  attr_accessor :new_visit

  def new_visit?
    @new_visit.present?
  end

  protected

  def generate_hid
    unless hid.present?
      update_attribute :hid, Digest::MD5.hexdigest("#{id}//#{DateTime.now}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
e9_crm-0.1.1 app/models/tracking_cookie.rb