How to install Facebook Pixel and track a conversion in an app using Cordova?
Asked Answered
S

2

8

How do you set the Facebook Pixel using an app that is made on Cordova. I need to track conversions in the app so I can better target the ads made in Facebook Ads. Normally it will be easy to do this task in an HTML page by copy pasting it and executing a JavaScript command, or in an app by installing the Facebook SDK and running a command using JAVA, Swift or Objective-C. However Ionic opens a web view where the user is not connected to Facebook, so it does not track it. So what is the way to go?

Salsala answered 15/6, 2016 at 15:23 Comment(0)
S
1

Add the Javascript from Facebook SDK Pixel into www/index.html (for permanent changes edit src/index.html)

<!-- 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', 'XXXXXXXXX'); 
fbq('track', 'PageView');
</script>
<noscript>
 <img height="1" width="1" 
src="https://www.facebook.com/tr?id=XXXXXXX&ev=PageView
&noscript=1"/>
</noscript>
<!-- End Facebook Pixel Code -->
Scofflaw answered 15/12, 2016 at 11:46 Comment(5)
But how will Facebook know the user comes from a Facebook ad?Salsala
Replace the XXXXXX with your Pixel code generated by your Ad that is a unique id.Scofflaw
Ok. But Facebook should know who actually the user is to optimize the campaign right?. Also where do I dynamically get the pixel code generated by the Ad from.Salsala
how will be urls tracked while opening different pages on app ? @AlejandroQHRodrickrodrigez
The www/index.html is replaced every build, it should be on src/index.htmlFeedback
R
0

We can add code for web only in index file.

Here is the useful doc

https://developers.facebook.com/docs/ads-for-websites/pixel-events/v3.1

Rodrickrodrigez answered 13/8, 2018 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.