API Reference

Overview

The nudge_core package is the core of the Nudge Flutter ecosystem. It tracks events, users, and app screens, and handles reporting to measure campaign performance. This package communicates with various plugins, which extend its functionality but cannot operate without it. nudge_core is a required dependency for every plugin.

Find the latest version of nudge_core here.

Installation

Add nudge_core to your pubspec.yaml file:

dependencies:
  nudge_core: ^latest_version

Run the following command to fetch the package:

flutter pub get

Import

Import the nudge_core package in your Dart file:

import 'package:nudge_core/nudge_core.dart';

Initialization

Create an instance of the Nudge class with your API_KEY:

 final core = Nudge(apiKey: <API_KEY>);

📘

Note:

You need a nudge account to get you API_KEY. See how to create one here.

Using NudgeProvider

Wrap your MaterialApp with the NudgeProvider to enable integration with Nudge.

Parameters for NudgeProvider

  • nudgeInstance (required): Pass the Nudge instance you initialized above.
  • plugins: Pass the plugins you want to use here.
  • navigatorKey (required): Pass your navigator key, or use NudgeProviderState.navigatorKey.
  • child (required): Pass your main MaterialApp widget here.

Using Your Own Navigator Key
If you have your own navigator key, pass it to both the NudgeProvider and MaterialApp:

return NudgeProvider(
  nudgeInstance: core,
  plugins: [],
  navigatorKey: <YOUR_NAVIGATOR_KEY>,
  child: MaterialApp(
    navigatorKey: <YOUR_NAVIGATOR_KEY>,
    title: 'Flutter Demo',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: Home(),
  ),
);

Using NudgeProviderState Navigator Key
If you don't have your own navigator key, you can use the one provided by NudgeProviderState:

return NudgeProvider(
  nudgeInstance: core,
  plugins: [],
  navigatorKey: NudgeProviderState.navigatorKey,
  child: MaterialApp(
    navigatorKey: NudgeProviderState.navigatorKey,
    title: 'Flutter Demo',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: Home(),
  ),
);

🚧

Note:

Ensure the same navigatorKey is passed to both NudgeProvider and MaterialApp

Identifying users

Initialize user sessions with the initSession method to identify users. Providing the externalId is crucial as it uniquely identifies the user.

await core.initSession(externalId:'CLIENT_IDENTIFIABLE_USER_ID');

Send User Properites
You can also send additional user properties for better targeting and segmentation:

await core.initSession('CLIENT_IDENTIFIABLE_USER_ID',
                  properties: <String, dynamic>{
                  "name": "Client User 1",
                  "age": 27,
                  "gender": "M",
                  "country":"US",
                  });

Tracking Events

Track user interactions by calling the track method. Ensure the session is initialized before tracking events:

await core.track(type:'EVENT_TYPE');

📘

Note:

  • Your event name should be one word and it cannot have any special characters except underscore ("_")
    For example : An event name "app-open" is not acceptable, but an event name "app_open" is.
  • Event name can only start with an alphabet.
    For example : An event name "1event" is not acceptable, but an event name "event1" is.
  • Event name cannot have more than 100 characters

Send Event Properties
You can add event properties to further define the interactions:

await core.track(type:'EVENT_TYPE',
                  properties: <String, dynamic>{
                  "product": "Fortune Cookies",
                  "quantity": 5,
                  "countryOfExport":"US",
                  });

That's it!