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)


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

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



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

def access_token
  @access_token
end

#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

#http_service_clientObject

The HttpServiceInterface client to use for executing the request.



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

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.



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

def namespace_id
  @namespace_id
end

#partner_agentObject

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



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

def partner_agent
  @partner_agent
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

#upload_idObject

Unique id used to denote the current set being uploaded.



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

def upload_id
  @upload_id
end

#upload_sourceObject

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



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

def upload_source
  @upload_source
end

#upload_tagObject

Tag string added to track your Offline event uploads.



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

def upload_tag
  @upload_tag
end

Instance Method Details

#==(o) ⇒ Object

Checks equality by comparing each attribute.



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

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



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

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


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

def eql?(o)
  self == o
end

#executeObject

Execute request



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

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



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

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



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

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



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

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



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

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



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

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

#to_sObject



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

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



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

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