Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Facebook pixel fire on a rails controller action

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
like image 725
SupremeA Avatar asked Jan 07 '17 03:01

SupremeA


People also ask

How do I know if my Facebook pixel is firing?

In order to check to see if your Facebook Pixel is firing properly, you'll want to use the Google Chrome browser. You can then install the Facebook Pixel Helper Chrome Extension, which is basically a tool that will show you if it detects a pixel on your website. You can download the Facebook Pixel Helper here.


1 Answers

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
like image 195
Andreas Avatar answered Sep 18 '22 21:09

Andreas