class FacebookAds::ServerSide::EventRequest

Attributes

events[RW]

An array of Server Event objects

http_service_client[RW]

The HttpServiceInterface client to use for executing the request.

namespace_id[RW]

Scope used to resolve extern_id or Third-party ID. Can be another data set or data partner ID.

partner_agent[RW]

Platform from which the event is sent e.g. wordpress

pixel_id[RW]

Ad pixel id

test_event_code[RW]

Code used to verify that your server events are received correctly by Facebook. Use this code to test your server events in the Test Events feature in Events Manager. See Test Events Tool (developers.facebook.com/docs/marketing-api/facebook-pixel/server-side-api/using-the-api#testEvents) for an example.

upload_id[RW]

Unique id used to denote the current set being uploaded.

upload_source[RW]

The origin/source of data for the dataset to be uploaded.

upload_tag[RW]

Tag string added to track your Offline event uploads.

Public Class Methods

new(pixel_id: nil, events: nil, test_event_code: nil, partner_agent: nil, namespace_id: nil, upload_id: nil, upload_tag: nil, upload_source: nil, http_service_client: nil) click to toggle source

@param [String] pixel_id @param [Array(FacebookAds::ServerSide::Event)] events @param [String] test_event_code @param [String] partner_agent @param [String] namespace_id @param [String] upload_id @param [String] upload_tag @param [String] upload_source @param [HttpServiceInterface] http_service_client

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 66
def initialize(pixel_id: nil, events: nil, test_event_code: nil, partner_agent: nil,
    namespace_id: nil, upload_id: nil, upload_tag: nil, upload_source: nil,
    http_service_client: nil)
  unless pixel_id.nil?
    self.pixel_id = pixel_id
  end
  unless events.nil?
    self.events = events
  end
  unless test_event_code.nil?
    self.test_event_code = test_event_code
  end
  unless partner_agent.nil?
    self.partner_agent = partner_agent
  end
  unless namespace_id.nil?
    self.namespace_id = namespace_id
  end
  unless upload_id.nil?
    self.upload_id = upload_id
  end
  unless upload_tag.nil?
    self.upload_tag = upload_tag
  end
  unless upload_source.nil?
    self.upload_source = upload_source
  end
  unless http_service_client.nil?
    self.http_service_client = http_service_client
  end
end

Public Instance Methods

==(o) click to toggle source

Checks equality by comparing each attribute.

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 231
def ==(o)
  return true if self.equal?(o)
  self.class == o.class &&
      pixel_id == o.pixel_id &&
      events == o.events &&
      test_event_code == o.test_event_code &&
      partner_agent == o.partner_agent &&
      namespace_id == o.namespace_id &&
      upload_id == o.upload_id &&
      upload_tag == o.upload_tag &&
      upload_source == o.upload_source &&
      http_service_client == o.http_service_client
end
build(attributes = {}) click to toggle source

build the object using the input hash @param [Hash] attributes attributes in the form of hash

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 100
def build(attributes = {})
  return unless attributes.is_a?(Hash)

  # convert string to symbol for hash key
  attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }

  if attributes.has_key?(:'pixel_id')
    self.pixel_id = attributes[:'pixel_id']
  end

  if attributes.has_key?(:'events')
    if (value = attributes[:'events']).is_a?(Array)
      self.events = value
    end
  end

  if attributes.has_key?(:'test_event_code')
    self.test_event_code = attributes[:'test_event_code']
  end

  if attributes.has_key?(:'partner_agent')
    self.partner_agent = attributes[:'partner_agent']
  end

  if attributes.has_key?(:'namespace_id')
    self.namespace_id = attributes[:'namespace_id']
  end

  if attributes.has_key?(:'upload_id')
    self.upload_id = attributes[:'upload_id']
  end

  if attributes.has_key?(:'upload_tag')
    self.upload_tag = attributes[:'upload_tag']
  end

  if attributes.has_key?(:'upload_source')
    self.upload_source = attributes[:'upload_source']
  end

  if attributes.has_key?(:'http_service_client')
    self.http_service_client = attributes[:'http_service_client']
  end
end
eql?(o) click to toggle source

@see the `==` method

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 246
def eql?(o)
  self == o
end
execute() click to toggle source

Execute request

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 146
def execute
  unless valid?
    raise list_invalid_properties
  end

  if http_service_client
    return execute_with_client(http_service_client)
  end
  params = get_params()
  params[:data] = normalize
  ads_pixel = FacebookAds::AdsPixel.get(pixel_id)
  response = ads_pixel.events.create(params)
  json_response_object = JSON.parse(JSON.generate(response), object_class: OpenStruct)
  FacebookAds::ServerSide::EventResponse.new(
      events_received: json_response_object.events_received,
      messages: json_response_object.messages,
      fbtrace_id: json_response_object.fbtrace_id
  )
end
execute_with_client(http_client) click to toggle source
# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 185
def execute_with_client http_client
  url = [
    "https://#{FacebookAds::DEFAULT_HOST}",
    FacebookAds::DEFAULT_API_VERSION,
    pixel_id,
    'events',
  ].join('/')
  headers = {
    'User-Agent' => "fbbizsdk-ruby-v#{FacebookAds::VERSION}"
  }
  params = get_params
  params[:data] = events.map(&:normalize)
  appsecret = FacebookAds.config.app_secret
  access_token = FacebookAds.config.access_token
  params[:access_token] = access_token
  if appsecret
    params[:appsecret_proof] = FacebookAds::ServerSide::HttpUtil.appsecret_proof(appsecret, access_token)
  end

  http_client.execute(
    url,
    FacebookAds::ServerSide::HttpMethod::POST,
    headers,
    params
  )
end
get_params() click to toggle source
# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 174
def get_params
  params = {}
  params[:test_event_code] = test_event_code unless test_event_code.nil?
  params[:partner_agent] = partner_agent unless partner_agent.nil?
  params[:namespace_id] = namespace_id unless namespace_id.nil?
  params[:upload_id] = upload_id unless upload_id.nil?
  params[:upload_tag] = upload_tag unless upload_tag.nil?
  params[:upload_source] = upload_source unless upload_source.nil?
  params
end
hash() click to toggle source

Calculates hash code according to all attributes. @return [Fixnum] Hash code

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 252
def hash
  [
    pixel_id,
    events,
    test_event_code,
    partner_agent,
    namespace_id,
    upload_id,
    upload_tag,
    upload_source,
    http_service_client,
  ].hash
end
list_invalid_properties() click to toggle source

Show invalid properties with the reasons. Usually used together with valid? @return Array for valid properties with the reasons

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 214
def list_invalid_properties
  invalid_properties = Array.new
  if @events.nil?
    invalid_properties.push('invalid value for "data", data cannot be nil.')
  end

  invalid_properties
end
normalize() click to toggle source
# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 166
def normalize
  normalized_events = []
  events.each do |event|
    normalized_events.push(JSON.generate(event.normalize))
  end
  normalized_events
end
to_s() click to toggle source
# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 266
def to_s
  hash = {}
  unless pixel_id.nil?
    hash['pixel_id'] = pixel_id
  end
  unless events.nil?
    hash['data'] = events
  end
  unless test_event_code.nil?
    hash['test_event_code'] = test_event_code
  end
  unless partner_agent.nil?
    hash['partner_agent'] = partner_agent
  end
  unless namespace_id.nil?
    hash['namespace_id'] = namespace_id
  end
  unless upload_id.nil?
    hash['upload_id'] = upload_id
  end
  unless upload_tag.nil?
    hash['upload_tag'] = upload_tag
  end
  unless upload_source.nil?
    hash['upload_source'] = upload_source
  end
  hash.to_s
end
valid?() click to toggle source

Check to see if the all the properties in the model are valid @return true if the model is valid

# File lib/facebook_ads/ad_objects/server_side/event_request.rb, line 225
def valid?
  return false if @events.nil?
  true
end