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

Adds eventID to Facebook Pixel #161

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
.DS_Store
.vscode
4 changes: 4 additions & 0 deletions lib/rack/tracker/facebook_pixel/facebook_pixel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def write
options.present? ? type_to_json << options_to_json : type_to_json
end

def with_event_id
", {\"eventID\":\"#{event_id}\"}" if event_id.present?
end

private

def type_to_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<% if events.any? %>
<script type="text/javascript">
<% events.each do |event| %>
fbq("<%= event.name %>", <%= event.write %>);
fbq("<%= event.name %>", <%= event.write %><%= event.with_event_id %>);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thank you for the contribution. I think this should be part of event#write.
Would also be more consistent with the other handlers.

<% end %>
</script>
<% end %>
2 changes: 1 addition & 1 deletion lib/rack/tracker/google_global/template/google_global.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if trackers.any? %>
<script async src='https://www.googletagmanager.com/gtag/js?id=<%= trackers[0][:id] %>'></script>
<script async src='https://<%= options[:host].presence || 'www.googletagmanager.com' %>/gtag/js?id=<%= trackers[0][:id] %>'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
Expand Down
13 changes: 6 additions & 7 deletions lib/rack/tracker/google_tag_manager/google_tag_manager.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Rack::Tracker::GoogleTagManager < Rack::Tracker::Handler

class Push < OpenStruct
def write
to_h.to_json
Expand All @@ -10,11 +9,11 @@ def inject(response)
# Sub! is enough, in well formed html there's only one head or body tag.
# Block syntax need to be used, otherwise backslashes in input will mess the output.
# @see http://stackoverflow.com/a/4149087/518204 and https://github.com/railslove/rack-tracker/issues/50
response.sub! %r{<head.*?>} do |m|
m.to_s << self.render_head
response.sub!(/<head.*?>/) do |m|
m.to_s << render_head
end
response.sub! %r{<body.*?>} do |m|
m.to_s << self.render_body
response.sub!(/<body.*?>/) do |m|
m.to_s << render_body
end
response
end
Expand All @@ -24,10 +23,10 @@ def container
end

def render_head
Tilt.new( File.join( File.dirname(__FILE__), 'template', 'google_tag_manager_head.erb') ).render(self)
Tilt.new(File.join(File.dirname(__FILE__), 'template', 'google_tag_manager_head.erb')).render(self)
end

def render_body
Tilt.new( File.join( File.dirname(__FILE__), 'template', 'google_tag_manager_body.erb') ).render(self)
Tilt.new(File.join(File.dirname(__FILE__), 'template', 'google_tag_manager_body.erb')).render(self)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if container %>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<%= container %>"
<noscript><iframe src="https://<%= options[:host].presence || 'www.googletagmanager.com' %>/ns.html?id=<%= container %>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
"https://<%= options[:host].presence || 'www.googletagmanager.com' %>/gtm.js?id="+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<%= container %>');</script>
<% end %>


2 changes: 1 addition & 1 deletion lib/rack/tracker/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Rack
class Tracker
VERSION = '1.12.1'
VERSION = '1.12.2'
end
end
27 changes: 27 additions & 0 deletions spec/handler/facebook_pixel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,31 @@ def env
expect(subject).to match(%r{https://www.facebook.com/tr\?id=&ev=PageView&noscript=1})
end
end

describe 'with event_id' do
def env
{
'tracker' => {
'facebook_pixel' =>
[
{
'type' => 'Purchase',
'class_name' => 'Track',
'options' =>
{
'value' => '23',
'currency' => 'EUR'
},
'event_id' => '42'
}
]
}
}
end
subject { described_class.new(env).render }

it 'will push the tracking event with the eventID param' do
expect(subject).to match(%r{"track", "Purchase", \{"value":"23","currency":"EUR"\}, \{"eventID":"42"\}})
end
end
end