optimizely_flutter_sdk 0.1.1-beta copy "optimizely_flutter_sdk: ^0.1.1-beta" to clipboard
optimizely_flutter_sdk: ^0.1.1-beta copied to clipboard

outdated

This repository houses the Flutter SDK for use with Optimizely Full Stack and Optimizely Rollouts.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
import 'package:optimizely_flutter_sdk/optimizely_flutter_sdk.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String uiResponse = 'Unknown';

  @override
  void initState() {
    super.initState();
    runOptimizelySDK();
  }

  Future<void> runOptimizelySDK() async {
    var flutterSDK = OptimizelyFlutterSdk("X9mZd2WDywaUL9hZXyh9A",
        datafilePeriodicDownloadInterval: 10 * 60,
        eventOptions: const EventOptions(
            batchSize: 1, timeInterval: 60, maxQueueSize: 10000));
    var response = await flutterSDK.initializeClient();

    setState(() {
      uiResponse = "Optimizely Client initialized: ${response.success} ";
    });

    var rng = Random();
    var randomUserName = "${rng.nextInt(1000)}";

    // Create user context
    var userContext = await flutterSDK.createUserContext(randomUserName);

    // Set attributes
    response = await userContext!.setAttributes({
      "age": 5,
      "doubleValue": 12.12,
      "boolValue": false,
      "stringValue": "121"
    });

    // To add decide listener
    var decideListenerId =
        await flutterSDK.addDecisionNotificationListener((notification) {
      print("Parsed decision event ....................");
      print(notification.type);
      print(notification.userId);
      print(notification);
      print("decide notification received");
    });

    // Decide call
    var decideResponse = await userContext.decide('flag1');
    uiResponse +=
        "\nFirst decide call variationKey: ${decideResponse.decision!.variationKey}";

    // should return following response without forced decision
    // flagKey: flag1
    // ruleKey: default-rollout-7371-20896892800
    // variationKey: off

    // Setting forced decision
    userContext.setForcedDecision(
        OptimizelyDecisionContext("flag1", "flag1_experiment"),
        OptimizelyForcedDecision("variation_a"));

    // Decide call
    decideResponse = await userContext.decide('flag1');
    uiResponse +=
        "\nSecond decide call variationKey: ${decideResponse.decision!.variationKey}";

    // should return following response with forced decision
    // flagKey: flag1
    // ruleKey: flag1_experiment
    // variationKey: variation_a

    // removing forced decision
    userContext.removeForcedDecision(
        OptimizelyDecisionContext("flag1", "flag1_experiment"));

    // Decide call
    decideResponse = await userContext.decide('flag1');
    uiResponse +=
        "\nThird decide call variationKey: ${decideResponse.decision!.variationKey}";

    setState(() {
      uiResponse = uiResponse;
    });

    // should return original response without forced decision
    // flagKey: flag1
    // ruleKey: default-rollout-7371-20896892800
    // variationKey: off

    // To cancel decide listener
    // await flutterSDK.removeNotificationListener(decideListenerId);

    // To add track listener
    var trackListenerID =
        await flutterSDK.addTrackNotificationListener((notification) {
      print("Parsed track event ....................");
      print(notification.attributes);
      print(notification.eventKey);
      print("track notification received");
    });

    // To add logEvent listener
    var logEventListenerId =
        await flutterSDK.addLogEventNotificationListener((notification) {
      print("Parsed log event ....................");
      print(notification.url);
      print(notification.params);
      print("log event notification received");
    });

    // Track call
    response = await userContext.trackEvent("myevent", {
      "age": 20,
      "doubleValue": 12.12,
      "boolValue": false,
      "stringValue": "121"
    });

    // To cancel track listener
    //await flutterSDK.removeNotificationListener(trackListenerID);

    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text(uiResponse),
        ),
      ),
    );
  }
}
7
likes
0
points
4.93k
downloads

Publisher

unverified uploader

Weekly Downloads

This repository houses the Flutter SDK for use with Optimizely Full Stack and Optimizely Rollouts.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on optimizely_flutter_sdk