How to make a Facebook pixel fire on a rails contr

2019-05-04 02:10发布

I have a ruby on rails app. I followed the facebook instructions to add a pixel.

However, to track a conversion, facebook requires you to put the page in the conversion that will appear when the desired result is reached. i.e if I want to show a customer has signed up, I would put the page you go to after signing up as the success object to track.

My issue is that in my application when a customer signs up, there is no landing page. The app takes the user back to the homepage. It shows a message on the homepage so I am trying to see if there is a way to track conversions from controller actions instead of actual pages.

The actions I need to count don't have pages, they are controller actions. Is there a gem, documentation, or a best practice on how to do this that anyone is aware of?

Here is the pixel that goes into the layouts file...

<!-- Facebook Pixel Code -->
      <script>
      !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
      n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
      n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
      t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
      document,'script','https://connect.facebook.net/en_US/fbevents.js');
      fbq('init', '18027724*****', {
      em: 'insert_email_variable,'
      });
      fbq('track', 'PageView');
      </script>
      <noscript><img height="1" width="1" style="display:none"
      src="https://www.facebook.com/tr?id=180277241*****&ev=PageView&noscript=1"
      /></noscript>
  <!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->

However I need it to fire here in the controller...

def create
    @reportapproval = current_user.reportapprovals.create(authorization_params)
      if @reportapproval.valid?
        if @reportapproval.manager_paying_report == true
          flash[:notice] = "Please make payment before proceeding"
          redirect_to new_managers_charge_path(id: @reportapproval.id)
        else
          flash[:notice] = "You have requested a report from #{@reportapproval.tenant_last_name}."
          redirect_to managers_dashboard_path
          @reportapproval.save
        end
     <----PIXEL FIRES HERE IN THE PROCESS ----->
      else
        flash[:error] = @reportapproval.errors.full_messages.to_sentence
        redirect_to managers_dashboard_path
      end
end

2条回答
男人必须洒脱
2楼-- · 2019-05-04 02:50

You can add a helper to check the current controller and action, then wrap displaying the tracking pixel in that. It would look something like:

# In ApplicationHelper
def display_tracking_pixel?(controller)
  controller.current_controller == "my_controller" && controller.current_action == "my_action"
end

# In the layout
<% if display_tracking_pixel?(controller) %>
<!-- Tracking embed goes here -->
<% end %>
查看更多
Root(大扎)
3楼-- · 2019-05-04 02:51

A cleaner solution might be to use the rack-tracker gem found here: https://github.com/railslove/rack-tracker

The master-branch doesn't yet have the new facebook pixel, so use this fork instead: https://github.com/jolim/rack-tracker

Add this to your gemfile:

gem 'rack-tracker', git: 'https://github.com/jolim/rack-tracker.git', ref: 'c2b1b30'

In your application.rb:

config.middleware.use(Rack::Tracker) do
  handler :facebook, { id: 'PIXEL_ID' }
end

Where you replace PIXEL_ID with your facebook pixel id.

Then in your controller:

def create
  ...
  # PIXEL FIRES HERE IN THE PROCESS
  tracker do |t|
    t.facebook :track, { type: 'Purchase', options: { value: 100, currency: 'USD' } }
  end
  ...
end
查看更多
登录 后发表回答