Class: Customer::GoogleLeadProcessor

Inherits:
BaseService show all
Defined in:
app/services/customer/google_lead_processor.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(lead_data) ⇒ Object

This is the lead data google sends
{
"lead_id": "TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
"user_column_data": [
{
"column_name": "Full Name",
"string_value": "FirstName LastName",
"column_id": "FULL_NAME"
},
{
"column_name": "User Email",
"string_value": "test@example.com",
"column_id": "EMAIL"
},
{
"column_name": "User Phone",
"string_value": "+16505550123",
"column_id": "PHONE_NUMBER"
},
{
"string_value": "Learning More about Installation",
"column_id": "SERVICE"
}
],
"api_version": "1.0",
"form_id": 21406785190,
"campaign_id": 16096035262,
"google_key": "test12345",
"is_test": true,
"gcl_id": "TeSter-123-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz-0123456789-AaBbCcDdEeFfGgHhIiJjKkLl",
"adgroup_id": 20000000000,
"creative_id": 30000000000
}



41
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/services/customer/google_lead_processor.rb', line 41

def process(lead_data)
  lead_data = lead_data.with_indifferent_access
  new_customer = Customer::NewCustomer.new
  new_customer.profile_id = ProfileConstants::HOMEOWNER
  country_iso = nil
  other_data = []
  lead_data[:user_column_data]&.each do |user_data|
    case user_data['column_id']
    when 'FULL_NAME'
      new_customer.person_name = user_data['string_value']
    when 'EMAIL'
      new_customer.email = user_data['string_value']
    when 'PHONE_NUMBER'
      new_customer.phone = user_data['string_value']
    when 'COMPANY_NAME'
      # Since users tend to do garbage, let's check that we have at least a decent length here
      if user_data['string_value'].presence&.size > 10
        new_customer.company_name = user_data['string_value']
        new_customer.profile_id = ProfileConstants::UNKNOWN_TRADE
      else
        other_data << "#{user_data['column_name'] || user_data['column_id']}: #{user_data['string_value']}"
      end
    when 'COUNTRY'
      # "column_name"=>"Country", "string_value"=>"United States", "column_id"=>"COUNTRY"}
      country_iso = Country.iso_for_string(user_data['string_value'])
    else
      other_data << "#{user_data['column_name'] || user_data['column_id']}: #{user_data['string_value']}"
    end
  end

  source_ref_code = nil
  # parse the special url arguments
  if lead_data[:args].present?
    logger.info "Parsing special args: #{lead_data[:args]}"
    lead_data[:args].split(',').each do |arg|
      key,val = arg.split(':')
      case key
      when 'rc'
        source_ref_code = val
        logger.info "Source ref code set to #{source_ref_code}"
      end
    end
  end

  # Try to guess catalog from phone and assign a country if lead form did not provide it
  phone = new_customer.phone.presence
  country_iso ||= PhoneNumber.parse(phone)&.country_iso if phone
  country_iso ||= LocaleUtility.locale_to_country_iso(I18n.locale) # The posting url gives us a clue
  country_iso ||= 'US' # Because we need a default
  new_customer.country_iso = country_iso

  # add google tracking stuff
  new_customer.gclid = lead_data[:gcl_id]
  # Set a google source (until we can do a proper campaign match)
  # Try a db match, first by the ref code if specified in url
  source = Source.find_by(referral_code: source_ref_code) if source_ref_code.present?
  source ||= Source.find_by(google_campaign_id: lead_data[:campaign_id])
  new_customer.source_id = source&.id || Source.find_by(id: 5059) # Google webhook

  new_customer.subscribe_to_announcements = true
  new_customer.subscribe_to_events = true
  new_customer.subscribe_to_newsletters = true
  new_customer.subscribe_to_promotions = true
  new_customer.subscribe_to_webinars = true

  # Add remaining fields to notes
  source_data = []
  source_data << "lead_id: #{lead_data[:lead_id]}"
  source_data << "form_id: #{lead_data[:form_id]}"
  source_data << "campaign_id: #{lead_data[:campaign_id]}"
  source_data << "adgroup_id: #{lead_data[:adgroup_id]}"
  source_data << "creative_id: #{lead_data[:creative_id]}"

  new_customer.source_info = "Google Lead Form - #{source_data.join(', ')}"
  new_customer.notes = other_data.join("\n")
  new_customer.rep_assignment_method = :dna # not assign
  new_customer.start_in_lead_qualify = true
  new_customer.creation_method = :api
  errors = []
  customer = nil

  PaperTrail.request(whodunnit: 'GoogleLeadProcessor') do
    customer = new_customer.build_and_save_customer(check_for_spam: true) # It's a webhook, bomb and report error with details
    errors = customer.errors.full_messages
  end
  Result.new(success: errors.empty?, errors: errors, customer: customer)
end