Class: Omnitrack::Adapters::Meta

Inherits:
Base
  • Object
show all
Defined in:
lib/omnitrack/adapters/meta.rb

Overview

Meta Conversions API (server-side events). Docs: developers.facebook.com/docs/marketing-api/conversions-api

Required config:

pixel_id       Meta Pixel ID
access_token   System User access token

Optional config:

test_event_code — for use with the Test Events tool in Events Manager
api_version     — default "v18.0"

Constant Summary collapse

API_BASE =
"https://graph.facebook.com"
API_VERSION =
"v18.0"
STANDARD_EVENTS =

Standard Meta event names — used for validation/mapping

%w[
  Purchase Lead CompleteRegistration AddToCart ViewContent
  Search InitiateCheckout AddPaymentInfo Subscribe
  Contact CustomizeProduct Donate FindLocation Schedule
  StartTrial SubmitApplication
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#enabled?, inherited, #initialize, #name

Constructor Details

This class inherits a constructor from Omnitrack::Adapters::Base

Instance Method Details

#identify_user(user_data = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/omnitrack/adapters/meta.rb', line 45

def identify_user(user_data = {})
  # Store normalised PII; will be sent with next track_event call
  ctx = Omnitrack::Context.current
  ctx&.merge!(meta_user_data: normalize_user(user_data))

  Omnitrack::Result.success(
    adapter:  name,
    metadata: { note: "Meta user data stored in context" }
  )
end

#track_conversion(data = {}) ⇒ Object



40
41
42
43
# File 'lib/omnitrack/adapters/meta.rb', line 40

def track_conversion(data = {})
  # Map to "Purchase" standard event by default
  track_event("Purchase", data)
end

#track_event(event_name, payload = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/omnitrack/adapters/meta.rb', line 30

def track_event(event_name, payload = {})
  safe_execute(event_name) do
    body     = build_event_body(event_name, payload)
    response = http_post(endpoint_url, body: body)
    result   = JSON.parse(response.body)

    Omnitrack::Result.success(adapter: name, data: result)
  end
end