diff --git a/lib/facebook_ads/ad_objects/server_side/event_request.rb b/lib/facebook_ads/ad_objects/server_side/event_request.rb index 0bcdb35c..51deb438 100644 --- a/lib/facebook_ads/ad_objects/server_side/event_request.rb +++ b/lib/facebook_ads/ad_objects/server_side/event_request.rb @@ -57,6 +57,9 @@ class EventRequest # The HttpServiceInterface client to use for executing the request. attr_accessor :http_service_client + # AdObject Session + attr_accessor :session + # @param [String] pixel_id # @param [Array(FacebookAds::ServerSide::Event)] events # @param [String] test_event_code @@ -69,7 +72,7 @@ class EventRequest # @param [HttpServiceInterface] http_service_client 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) + access_token: nil, http_service_client: nil, session: nil) unless pixel_id.nil? self.pixel_id = pixel_id end @@ -100,6 +103,9 @@ def initialize(pixel_id: nil, events: nil, test_event_code: nil, partner_agent: unless http_service_client.nil? self.http_service_client = http_service_client end + unless session.nil? + self.session = session + end end # build the object using the input hash @@ -160,7 +166,7 @@ def execute end params = get_params() params[:data] = normalize - session = FacebookAds::Session.new(access_token: access_token) if access_token + session = self.session || (access_token ? FacebookAds::Session.new(access_token: access_token) : nil) 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) diff --git a/lib/facebook_ads/session.rb b/lib/facebook_ads/session.rb index 28c19375..ceadf334 100644 --- a/lib/facebook_ads/session.rb +++ b/lib/facebook_ads/session.rb @@ -11,16 +11,18 @@ module FacebookAds class Session - attr_accessor :access_token, :app_secret, :api_version, :server_host + attr_accessor :access_token, :app_secret, :api_version, :server_host, :connection_setup def initialize(access_token: nil, app_secret: nil, server_host: FacebookAds.config.server_host, - api_version: FacebookAds.config.api_version) + api_version: FacebookAds.config.api_version, + &connection_setup) @access_token = access_token @app_secret = app_secret @server_host = server_host @api_version = api_version + @connection_setup = connection_setup end def request(method, path, params = nil) @@ -52,7 +54,7 @@ def api_conn # TODO Json Request # TODO URL Encode - stringify json faraday.request :url_encoded - + @connection_setup&.call(faraday) faraday.response :logger, Utils.logger, bodies: FacebookAds.config.log_api_bodies faraday.adapter Faraday.default_adapter end