1

I need to run a Javascript code in my Flutter app. When user press one button i need to run this code:

<!-- 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', '24023562411360');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=24035634111&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->

It is possible?

Now i'm trying this from flutter_webview_plugin, but i thinks isn't working:

flutterWebviewPlugin.evalJavascript(
          "<!-- 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', '24022385634361');fbq('track', 'PageView');</script><noscript><img height='1' width='1' style='display:none'src='https://www.facebook.com/tr?id=24022385634361&ev=PageView&noscript=1'/></noscript><!-- End Facebook Pixel Code -->");
2
  • 1
    Does this answer your question? Calling a javascript function from flutter app Commented Jul 23, 2020 at 19:17
  • I'm not familiar with these libraries, but maybe try removing the HTML tags around the JS code? i.e., remove <!-- Facebook Pixel Code --><script> and </script><noscript><img height='1' width='1' style='display:none'src='https://www.facebook.com/tr?id=24022385634361&ev=PageView&noscript=1'/></noscript><!-- End Facebook Pixel Code --> Commented Jul 24, 2020 at 5:54

2 Answers 2

1

AFAIK, there are plugins currently available to call a js function. See examples from the following plugins:

Check out this blog "JavaScript with Flutter | Is it possible?" to give you guide on implementation of js inside your Flutter app.

Sign up to request clarification or add additional context in comments.

Comments

0

To log events on Flutter Web, you firstly need to configure your Facebook Pixel inserting the snippet above </head> in web/index.html. You can find the snippet following this guide

After that, you can send events to your pixel utilizing dart interoperability. More informations here

import 'dart:developer';
import 'dart:js_interop_unsafe';
import 'dart:js_interop';

@JS()
external void fbq(String command, String eventName, JSAny? parameters);

class Tracking {
  static void sendEvent(
      {required String eventName, Map<String, dynamic>? parameters}) {
    //Meta Pixel
    try {
      final args = parameters?.jsify();
      fbq('trackCustom', eventName, args);
    } catch (e) {
      log(e.toString());
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.