Core SDK Integration
Overview
The nudgecore_v2
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. nudgecore_v2
is a required dependency for every plugin.
Find the latest version of nudgecore_v2
here.
Installation
Add nudgecore_v2
to your pubspec.yaml
file:
dependencies:
nudgecore_v2: ^latest_version
Run the following command to fetch the package:
flutter pub get
Import
Import the nudgecore_v2
package in your Dart file:
import 'package:nudgecore_v2/core/Nudge.dart';
Initialization
Create an instance of the Nudge
class with your API_KEY
:
Nudge nudge = Nudge(
apiKey: "API_KEY",
debugMode: true);
📘 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 useNudgeProviderState.navigatorKey.
child
(required): Pass your mainMaterialApp
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: nudge,
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: nudge,
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 bothNudgeProvider
andMaterialApp
Identifying users
Initialize user sessions with the userIdentifier
method to identify users. Providing the externalId is crucial as it uniquely identifies the user.
nudge.userIdentifier(externalId: "EXTERNAL_ID");
or
Nudge.getInstance().userIdentifier(externalId: "EXTERNAL_ID");
Send User Properties
You can also send additional user properties for better targeting and segmentation:
Map<String,dynamic>? props = {
"tier" : "Gold",
"location" : "India",
"subscribe" : "No"
};
nudge.userIdentifier(externalId: "EXTERNAL_ID",name: "NAME",email: "EMAIL",phoneNumber: "PHONE_NUMBER",properties: props);
or
Nudge.getInstance().userIdentifier(externalId: "EXTERNAL_ID",name: "NAME",email: "EMAIL",phoneNumber: "PHONE_NUMBER",properties: props);
Tracking Events
Track user interactions by calling the track
method. Ensure the session is initialized before tracking events:
Nudge.getInstance().track(event: "EVENT_NAME");
or
nudge.track(event: "EVENT_NAME");
📘 Important:
- 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
🚧 Warning:
- Do not use
Nudge.getInstance().track()
ornudge.track()
insidebuild
method of a stateful widget.
Send Event Properties
You can add event properties to further define the interactions:
Map<String,dynamic>? props = {
"tier" : "Gold",
"location" : "India",
"subscribe" : "No"
};
Nudge.getInstance().track(event: "EVENT_NAME",properties: props);
or
nudge.track(event: "EVENT_NAME",properties: props);