Class: FacebookAds::ServerSide::EventRequest

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

Direct Known Subclasses

EventRequestAsync

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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, access_token: nil, http_service_client: nil) ⇒ EventRequest

Returns a new instance of EventRequest.

Parameters:

  • pixel_id (String) (defaults to: nil)
  • events (Array(FacebookAds::ServerSide::Event)) (defaults to: nil)
  • test_event_code (String) (defaults to: nil)
  • partner_agent (String) (defaults to: nil)
  • namespace_id (String) (defaults to: nil)
  • upload_id (String) (defaults to: nil)
  • upload_tag (String) (defaults to: nil)
  • upload_source (String) (defaults to: nil)
  • access_token (String) (defaults to: nil)
  • http_service_client (HttpServiceInterface) (defaults to: nil)


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
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 71

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,
    access_token: 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 access_token.nil?
    self.access_token = access_token
  end
  unless http_service_client.nil?
    self.http_service_client = http_service_client
  end
end

Instance Attribute Details

#access_tokenObject

Access token to use for AdObject Session



56
57
58
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 56

def access_token
  @access_token
end

#eventsObject

An array of Server Event objects



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

def events
  @events
end

#http_service_clientObject

The HttpServiceInterface client to use for executing the request.



59
60
61
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 59

def http_service_client
  @http_service_client
end

#namespace_idObject

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



44
45
46
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 44

def namespace_id
  @namespace_id
end

#partner_agentObject

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



41
42
43
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 41

def partner_agent
  @partner_agent
end

#pixel_idObject

Ad pixel id



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

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.



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

def test_event_code
  @test_event_code
end

#upload_idObject

Unique id used to denote the current set being uploaded.



47
48
49
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 47

def upload_id
  @upload_id
end

#upload_sourceObject

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



53
54
55
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 53

def upload_source
  @upload_source
end

#upload_tagObject

Tag string added to track your Offline event uploads.



50
51
52
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 50

def upload_tag
  @upload_tag
end

Instance Method Details

#==(o) ⇒ Object

Checks equality by comparing each attribute.



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 240

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 = {}) ⇒ Object

build the object using the input hash

Parameters:

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

    attributes in the form of hash



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 108

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) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • `==` method


255
256
257
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 255

def eql?(o)
  self == o
end

#executeObject

Execute request



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 154

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
  session = FacebookAds::Session.new(access_token: access_token) if access_token
  ads_pixel = FacebookAds::AdsPixel.get(pixel_id, session)
  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) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 194

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_paramsObject



183
184
185
186
187
188
189
190
191
192
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 183

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

#hashFixnum

Calculates hash code according to all attributes.

Returns:

  • (Fixnum)

    Hash code



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 261

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_propertiesObject

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

Returns:

  • Array for valid properties with the reasons



223
224
225
226
227
228
229
230
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 223

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



175
176
177
178
179
180
181
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 175

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

#to_sObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 275

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?Boolean

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

Returns:

  • (Boolean)

    true if the model is valid



234
235
236
237
# File 'lib/facebook_ads/ad_objects/server_side/event_request.rb', line 234

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