Class: Octo::Funnel

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document, Record
Defined in:
lib/octocore-mongo/models/enterprise/funnels.rb

Overview

Stores the funnel for the enterprise

Constant Summary

Constants included from MongoMapper::Document

MongoMapper::Document::DUMP_ATTRS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Record

#unique_id

Methods included from MongoMapper::Document

#marshal_dump, #marshal_load, redis, update_cache_config

Class Method Details

.from_pages(pages, opts = {}) ⇒ Octo::Funnel

Generates a new funnel from the pages provided

Parameters:

  • pages (Array)

    The pages array. This array could contain instances of either String, Octo::Product or Octo::Page. If string, it will be assumed that these are routeurls for pages or products. If the class is explicitly specified, it will be used.

  • opts (Hash) (defaults to: {})

    The options for creating funnel

Options Hash (opts):

  • :name (String)

    The name of the funnel

  • :enterprise_id (String)

    The enterprise ID for whom funnel is being created

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/octocore-mongo/models/enterprise/funnels.rb', line 50

def self.from_pages(pages, opts = {})
  funnel_length = pages.count
  return nil if funnel_length.zero?

  funnel = Array.new
  enterprise_id = opts.fetch(:enterprise_id, nil)

  # Check if they are Octo::Product or Octo::Page instantces and handle
  if ::Set.new([Octo::Product, Octo::Page]).include?(pages[0].class)
    funnel = pages.collect { |p| p.routeurl }
    enterprise_id = pages[0].enterprise_id
  elsif pages[0].class == String
    funnel = pages
  end

  # Create a new funnel
  self.new(
    enterprise_id: enterprise_id,
    name: opts.fetch(:name),
    funnel: funnel
  ).save!
end

Instance Method Details

#activate_funnelObject

Activates a funnel



35
36
37
# File 'lib/octocore-mongo/models/enterprise/funnels.rb', line 35

def activate_funnel
  self.active = true
end

#create_name_slugObject

Creates name slug



30
31
32
# File 'lib/octocore-mongo/models/enterprise/funnels.rb', line 30

def create_name_slug
  self.name_slug = self.name.to_slug
end

#data(ts = Time.now.floor) ⇒ Octo::FunnelData

Returns data for a funnel

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/octocore-mongo/models/enterprise/funnels.rb', line 91

def data(ts = Time.now.floor)
  args = {
    enterprise_id: self.enterprise._id,
    funnel_slug: self.name_slug,
    ts: ts
  }
  res = Octo::FunnelData.where(args)
  if res.count > 0
    res.first
  elsif self.enterprise.fakedata?
    args.merge!({ value: fake_data(self.funnel.count) })
    Octo::FunnelData.new(args).save!
  end
end

#populate_with_fake_data(interval_days = 7) ⇒ Object

Populates a newly created funnel with some fake data

Parameters:

  • days (Fixnum)

    The number of days for which data to be faked



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/octocore-mongo/models/enterprise/funnels.rb', line 75

def populate_with_fake_data(interval_days = 7)
  if self.enterprise.fakedata?
    today = Time.now.beginning_of_day
    (today - interval_days.days).to(today, 24.hour).each do |ts|
      Octo::FunnelData.new(
        enterprise_id: self.enterprise_id,
        funnel_slug: self.name_slug,
        ts: ts,
        value: fake_data(self.funnel.count)
      ).save!
    end
  end
end