Skip to main content
Version: Flutter SDK

Examples

Practical, task-based code snippets for the Flutter SDK.

Basic setupโ€‹

If the user is already authenticated when the app loads, pass their identity directly to initSdk() to avoid creating an anonymous user:

import 'package:screeb_sdk_flutter/screeb_sdk_flutter.dart';

@override
void initState() {
super.initState();
PluginScreeb.initSdk(
"<YOUR-CHANNEL-ID>",
userId: "<USER-ID>",
properties: <String, dynamic>{
'email': "support@screeb.app",
'plan': "pro",
},
);
}

If the user isn't authenticated yet, init without identity and call setIdentity() after login (see below).

PluginScreeb.initSdk("<YOUR-CHANNEL-ID>");

Identify a user after loginโ€‹

When a user logs in post-init, identify them:

PluginScreeb.setIdentity("user-123", properties: <String, dynamic>{
'email': "support@screeb.app",
'plan': "premium",
'signup_date': DateTime.parse("2024-01-15"),
});

Track an eventโ€‹

Track user actions with optional properties for context:

// Simple event
PluginScreeb.trackEvent("button_clicked");

// Event with properties
PluginScreeb.trackEvent("checkout_completed", properties: <String, dynamic>{
'amount': 99.99,
'currency': "USD",
'items_count': 3,
'payment_method': "credit_card",
});

Start a surveyโ€‹

Trigger a survey programmatically when a user performs an action:

// Start a specific survey
PluginScreeb.startSurvey("<SURVEY-ID>");

// Prevent multiple responses
PluginScreeb.startSurvey("<SURVEY-ID>", allowMultipleResponses: false);

// Add hidden fields for context
PluginScreeb.startSurvey("<SURVEY-ID>", properties: <String, dynamic>{
'source': "checkout_page",
'cart_value': 149.99,
});

Use hooksโ€‹

Set up hooks during initialization to react to survey events:

import 'dart:developer';

PluginScreeb.initSdk(
"<YOUR-CHANNEL-ID>",
hooks: <String, dynamic>{
"version": "1.0.0",
"onReady": (dynamic data) {
log("Screeb SDK is ready");
log(data.toString());
},
"onSurveyCompleted": (dynamic data) {
log("Survey completed");
log(data.toString());
// Track completion in your analytics
// Show a thank you message
},
"onSurveyDisplayAllowed": (dynamic data) {
log("Survey about to display");
log(data.toString());
// Return false to prevent display
return true;
},
},
);

Reset on logoutโ€‹

Clear the current user identity when they log out:

void handleLogout() {
// Clear user session
PluginScreeb.resetIdentity();
// Navigate to login screen
}