Class: FacebookAds::ServerSide::EventRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_ads/ad_objects/server_side/event_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pixel_id: nil, events: nil, test_event_code: nil) ⇒ EventRequest

Returns a new instance of EventRequest.

Parameters:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 42

def initialize(pixel_id: nil, events: nil, test_event_code: 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
end

Instance Attribute Details

#eventsObject

An array of Server Event objects



28
29
30
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 28

def events
  @events
end

#pixel_idObject

Ad pixel id



37
38
39
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 37

def pixel_id
  @pixel_id
end

#test_event_codeObject

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.



34
35
36
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 34

def test_event_code
  @test_event_code
end

Instance Method Details

#==(o) ⇒ Object

Checks equality by comparing each attribute.



125
126
127
128
129
130
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 125

def ==(o)
  return true if self.equal?(o)
  self.class == o.class &&
      events == o.events &&
      test_event_code == o.test_event_code
end

#build(attributes = {}) ⇒ Object

build the object using the input hash

Parameters:

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

    attributes in the form of hash



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 56

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
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • `==` method


133
134
135
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 133

def eql?(o)
  self == o
end

#executeObject

Execute request



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 78

def execute
  unless valid?
    raise list_invalid_properties
  end
  normalized_events = normalize
  ads_pixel = FacebookAds::AdsPixel.get(pixel_id)
  response = ads_pixel.events.create(
      {
          data: normalized_events,
          test_event_code: test_event_code
      }
  )
  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

#hashFixnum

Calculates hash code according to all attributes.

Returns:

  • (Fixnum)

    Hash code



139
140
141
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 139

def hash
  [events, test_event_code].hash
end

#list_invalid_propertiesObject

Show invalid properties with the reasons. Usually used together with valid?

Returns:

  • Array for valid properties with the reasons



108
109
110
111
112
113
114
115
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 108

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

#normalizeObject



98
99
100
101
102
103
104
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 98

def normalize
  normalized_events = []
  events.each do |event|
    normalized_events.push(JSON.generate(event.normalize))
  end
  normalized_events
end

#to_sObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 143

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
  hash.to_s
end

#valid?Boolean

Check to see if the all the properties in the model are valid

Returns:

  • (Boolean)

    true if the model is valid



119
120
121
122
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 119

def valid?
  return false if @events.nil?
  true
end