diff --git a/README.md b/README.md index e1b7927..1f8c8e4 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,7 @@ To add events or variables to the dataLayer from the server side, just call the ### Facebook * `Facebook Pixel` - adds the [Facebook Pixel](https://www.facebook.com/business/help/952192354843755) +* `:advanced_matching` - sets optional matching parameters, see https://developers.facebook.com/docs/facebook-pixel/advanced/advanced-matching/ for details. Use in conjunction with the [Facebook Helper](https://developers.facebook.com/docs/facebook-pixel/pixel-helper) to confirm your event fires correctly. @@ -316,12 +317,13 @@ First, add the following to your config: ```ruby config.middleware.use(Rack::Tracker) do - handler :facebook_pixel, { id: 'PIXEL_ID' } + handler :facebook_pixel, { id: 'PIXEL_ID', advanced_matching: {fn: "Bob"} } end ``` #### Dynamic Pixel Configuration + If you need to have different pixel ids e.g. based on the request or serving pages for different accounts, you have the possibility to achieve this by passing a lambda: ```ruby @@ -340,6 +342,10 @@ and set the pixel id within the request `env` variable. Here an example on how i end ``` + + + + #### Standard Events To track Standard Events from the server side just call the `tracker` method in your controller. diff --git a/lib/rack/tracker/facebook_pixel/facebook_pixel.rb b/lib/rack/tracker/facebook_pixel/facebook_pixel.rb index f6015dd..d88b0d1 100644 --- a/lib/rack/tracker/facebook_pixel/facebook_pixel.rb +++ b/lib/rack/tracker/facebook_pixel/facebook_pixel.rb @@ -1,6 +1,6 @@ class Rack::Tracker::FacebookPixel < Rack::Tracker::Handler self.position = :body - self.allowed_tracker_options = [:id] + self.allowed_tracker_options = [:id, :advanced_matching] class Event < OpenStruct def write diff --git a/lib/rack/tracker/facebook_pixel/template/facebook_pixel.erb b/lib/rack/tracker/facebook_pixel/template/facebook_pixel.erb index 2901c52..e1248ef 100644 --- a/lib/rack/tracker/facebook_pixel/template/facebook_pixel.erb +++ b/lib/rack/tracker/facebook_pixel/template/facebook_pixel.erb @@ -7,7 +7,7 @@ t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script','//connect.facebook.net/en_US/fbevents.js'); - fbq('init', '<%= tracker_options[:id] %>'); + fbq('init', '<%= tracker_options[:id] %>', <%= tracker_options[:advanced_matching].to_h.compact.to_json %>); fbq('track', "PageView"); } diff --git a/spec/handler/facebook_pixel_spec.rb b/spec/handler/facebook_pixel_spec.rb index 9716529..3d43c21 100644 --- a/spec/handler/facebook_pixel_spec.rb +++ b/spec/handler/facebook_pixel_spec.rb @@ -12,7 +12,7 @@ def env subject { described_class.new(env, id: 'PIXEL_ID').render } it 'will push the tracking events to the queue' do - expect(subject).to match(%r{fbq\('init', 'PIXEL_ID'\)}) + expect(subject).to match(%r{fbq\('init', 'PIXEL_ID', {}\)}) end it 'will add the noscript fallback' do @@ -24,7 +24,7 @@ def env subject { described_class.new(env, id: lambda { |env| env['PIXEL_ID'] }).render } it 'will push the tracking events to the queue' do - expect(subject).to match(%r{fbq\('init', 'DYNAMIC_PIXEL_ID'\)}) + expect(subject).to match(%r{fbq\('init', 'DYNAMIC_PIXEL_ID', {}\)}) end it 'will add the noscript fallback' do diff --git a/spec/integration/facebook_pixel_integration_spec.rb b/spec/integration/facebook_pixel_integration_spec.rb index 07af9f0..d935305 100644 --- a/spec/integration/facebook_pixel_integration_spec.rb +++ b/spec/integration/facebook_pixel_integration_spec.rb @@ -1,6 +1,8 @@ require 'support/capybara_app_helper' RSpec.describe "Facebook Pixel Integration" do + subject { page } + before do setup_app(action: :facebook_pixel) do |tracker| tracker.handler :facebook_pixel, { id: 'PIXEL_ID' } @@ -8,10 +10,10 @@ visit '/' end - subject { page } + it "embeds the script tag with tracking event from the controller action" do - expect(page).to have_content("fbq('init', 'PIXEL_ID');") + expect(page).to have_content("fbq('init', 'PIXEL_ID', {});") expect(page.body).to include('https://www.facebook.com/tr?id=PIXEL_ID&ev=PageView&noscript=1') end @@ -23,4 +25,19 @@ it "can use non-standard event names for audience building" do expect(page.body).to match(/fbq\("trackCustom", "FrequentShopper", {\"purchases\":24,\"category\":\"Sport\"}/) end + + describe "swith advanced matching" do + before do + setup_app(action: :facebook_pixel) do |tracker| + tracker.handler :facebook_pixel, { id: 'PIXEL_ID', advanced_matching: {fn: "John", ln: "Smith"} } + end + visit '/' + end + + + it 'adding advanced matching on pixel init' do + expect(page).to have_content("fbq('init', 'PIXEL_ID', {\"fn\":\"John\",\"ln\":\"Smith\"});") + end + + end end