Skip to main content

facebook_app_events 0.31.0 copy "facebook_app_events: ^0.31.0" to clipboard
facebook_app_events: ^0.31.0 copied to clipboard

Flutter plugin for Facebook App Events, an app measurement solution that provides insight on app usage and user engagement in Facebook Analytics.

example/lib/main.dart

import 'package:facebook_app_events/facebook_app_events.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static final facebookAppEvents = FacebookAppEvents();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const _ConsentGate(),
                MaterialButton(
                  child: Text("Click me!"),
                  onPressed: () {
                    facebookAppEvents.logEvent(
                      name: 'button_clicked',
                      parameters: {
                        'button_id': 'the_clickme_button',
                      },
                    );
                  },
                ),
                MaterialButton(
                  child: Text("Set user data"),
                  onPressed: () {
                    facebookAppEvents.setUserData(
                      email: 'opensource@oddbit.id',
                      firstName: 'Oddbit',
                      city: 'Denpasar',
                      country: 'Indonesia',
                      externalId: 'example-user-42',
                    );
                  },
                ),
                MaterialButton(
                  child: Text("Test logAddToCart"),
                  onPressed: () {
                    facebookAppEvents.logAddToCart(
                      id: '1',
                      type: 'product',
                      price: 99.0,
                      currency: 'TRY',
                    );
                  },
                ),
                MaterialButton(
                  child: Text("Test purchase!"),
                  onPressed: () {
                    facebookAppEvents.logPurchase(amount: 1, currency: "USD");
                  },
                ),
                MaterialButton(
                  child: Text("Enable advertiser ID collection"),
                  onPressed: () {
                    facebookAppEvents.setAdvertiserIdCollectionEnabled(true);
                  },
                ),
                MaterialButton(
                  child: Text("Disable advertiser ID collection"),
                  onPressed: () {
                    facebookAppEvents.setAdvertiserIdCollectionEnabled(false);
                  },
                ),
                MaterialButton(
                  child: Text("Limit event and data usage"),
                  onPressed: () {
                    facebookAppEvents.setLimitEventAndDataUsage(true);
                  },
                ),
                MaterialButton(
                  child: Text("Enable Limited Data Use (LDU)"),
                  onPressed: () {
                    facebookAppEvents.setDataProcessingOptions(
                      ['LDU'],
                      country: 0,
                      state: 0,
                    );
                  },
                ),
                MaterialButton(
                  child: Text("Log product item"),
                  onPressed: () {
                    facebookAppEvents.logProductItem(
                      itemId: 'SKU-1',
                      availability: ProductAvailability.inStock,
                      condition: ProductCondition.newItem,
                      description: 'Comfortable running shoes',
                      imageLink: 'https://example.com/shoes.png',
                      link: 'https://example.com/shoes',
                      title: 'Running Shoes',
                      priceAmount: 79.99,
                      currency: 'USD',
                      gtin: '0123456789012',
                    );
                  },
                ),
                MaterialButton(
                  child: Text("Flush events explicitly only"),
                  onPressed: () {
                    facebookAppEvents
                        .setFlushBehavior(FlushBehavior.explicitOnly);
                  },
                ),
                MaterialButton(
                  child: Text("Register push token"),
                  onPressed: () {
                    facebookAppEvents
                        .setPushNotificationsDeviceToken('example-token');
                  },
                ),
                MaterialButton(
                  child: Text("Clear email user data"),
                  onPressed: () {
                    facebookAppEvents
                        .clearUserDataForType(FacebookUserDataField.email);
                  },
                ),
                MaterialButton(
                  child: Text("Enable SDK debug logging"),
                  onPressed: () {
                    facebookAppEvents.setDebugLoggingEnabled(true);
                  },
                ),
                MaterialButton(
                  child: Text("Log search"),
                  onPressed: () {
                    facebookAppEvents.logSearched(
                      searchString: 'running shoes',
                      contentType: 'product',
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// Demonstrates deferred SDK initialization.
///
/// This example turns automatic initialization off on both platforms
/// (`FacebookAutoInitEnabled` in `Info.plist`,
/// `com.facebook.sdk.AutoInitEnabled` in `AndroidManifest.xml`), the way a
/// consent-gated app would. Nothing below the button works on iOS until it has
/// been pressed: every other call fails with a `PlatformException` of code
/// `sdk_not_initialized`. The anonymous ID is only read after initialization
/// because reading it creates and persists the ID on iOS.
class _ConsentGate extends StatefulWidget {
  const _ConsentGate();

  @override
  State<_ConsentGate> createState() => _ConsentGateState();
}

class _ConsentGateState extends State<_ConsentGate> {
  Future<String?>? _anonymousId;

  Future<void> _initialize() async {
    await MyApp.facebookAppEvents.initializeSdk();
    setState(() {
      _anonymousId = MyApp.facebookAppEvents.getAnonymousId();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        MaterialButton(
          onPressed: _initialize,
          child: const Text("Initialize SDK (consent given)"),
        ),
        FutureBuilder(
          future: _anonymousId,
          builder: (context, snapshot) {
            if (_anonymousId == null) {
              return const Text('SDK not initialized');
            }
            final id = snapshot.data ?? '???';
            return Text('Anonymous ID: $id');
          },
        ),
      ],
    );
  }
}
347
likes
160
points
235k
downloads

Documentation

API reference

Publisher

verified publisheroddbit.id

Weekly Downloads

Flutter plugin for Facebook App Events, an app measurement solution that provides insight on app usage and user engagement in Facebook Analytics.

Homepage
Repository (GitHub)
View/report issues
Contributing

License

Apache-2.0 (license)

Dependencies

flutter

More

Packages that depend on facebook_app_events

Packages that implement facebook_app_events