Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fb advanced matching #157

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,21 @@ 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.

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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/tracker/facebook_pixel/facebook_pixel.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions spec/handler/facebook_pixel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 19 additions & 2 deletions spec/integration/facebook_pixel_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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' }
end
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

Expand All @@ -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