Example #1
0
        public async Task TestLocalAnalytics()
        {
            int eventCount = 10000;

            // Create a LocalAnalytics instance that uses only memory stores for testing:
            LocalAnalytics localAnalytics = new LocalAnalytics(new InMemoryKeyValueStore());

            localAnalytics.createStoreFor = (_) => new InMemoryKeyValueStore().GetTypeAdapter <AppFlowEvent>();

            // Pass this local analytics system to the app flow impl. as the target store:
            AppFlowToStore appFlow = new AppFlowToStore(localAnalytics);

            await TestAppFlowWithStore(eventCount, appFlow); // Run the tests

            // Get the store that contains only the events of a specific category:
            var catMethodStore = localAnalytics.GetStoreForCategory(EventConsts.catMethod);
            { // Check that all events so far are of the method category:
                var all = await localAnalytics.GetAll();

                var allForCat = await catMethodStore.GetAll();

                Assert.Equal(all.Count(), allForCat.Count());
            }
            { // Add an event of a different category and check that the numbers again:
                appFlow.TrackEvent(EventConsts.catUi, "Some UI event");
                var all = await localAnalytics.GetAll();

                var allForCat = await catMethodStore.GetAll();

                Assert.Equal(all.Count(), allForCat.Count() + 1);
                var catUiStore = localAnalytics.GetStoreForCategory(EventConsts.catUi);
                Assert.Single(await catUiStore.GetAll());
            }
        }
Example #2
0
 public async Task UpdateCurrentCategoryCounts()
 {
     foreach (var xpFactor in xpFactors)
     {
         var category = xpFactor.Key;
         var store    = analytics.GetStoreForCategory(category);
         var currentCountForCategory = (await store.GetAllKeys()).Count();
         cachedCategoryCounts.AddOrReplace(category, currentCountForCategory);
     }
 }
Example #3
0
        public async Task TestProgressiveDisclosure()
        {
            // Get your key from https://console.developers.google.com/apis/credentials
            var apiKey = "AIzaSyCtcFQMgRIUHhSuXggm4BtXT4eZvUrBWN0";
            // https://docs.google.com/spreadsheets/d/1KBamVmgEUX-fyogMJ48TT6h2kAMKyWU1uBL5skCGRBM contains the sheetId:
            var sheetId           = "1KBamVmgEUX-fyogMJ48TT6h2kAMKyWU1uBL5skCGRBM";
            var sheetName         = "MySheet1"; // Has to match the sheet name
            var googleSheetsStore = new GoogleSheetsKeyValueStore(new InMemoryKeyValueStore(), apiKey, sheetId, sheetName);
            var testStore         = new FeatureFlagStore(new InMemoryKeyValueStore(), googleSheetsStore);

            IoC.inject.SetSingleton <FeatureFlagManager <FeatureFlag> >(new FeatureFlagManager <FeatureFlag>(testStore));

            // Make sure user would normally be included in the rollout:
            var flagId4 = "MyFlag4";
            var flag4   = await FeatureFlagManager <FeatureFlag> .instance.GetFeatureFlag(flagId4);

            Assert.Equal(flagId4, flag4.id);
            Assert.Equal(100, flag4.rolloutPercentage);

            // There is no user progression system setup so the requiredXp value of the feature flag is ignored:
            Assert.True(await FeatureFlag.IsEnabled(flagId4));
            Assert.True(await flag4.IsFeatureUnlocked());

            // Setup progression system and check again:
            var xpSystem = new TestXpSystem();

            IoC.inject.SetSingleton <IProgressionSystem <FeatureFlag> >(xpSystem);
            // Now that there is a progression system
            Assert.False(await flag4.IsFeatureUnlocked());
            Assert.False(await FeatureFlag.IsEnabled(flagId4));

            var eventCount = 1000;

            var store = new MutationObserverKeyValueStore().WithFallbackStore(new InMemoryKeyValueStore());

            // Lets assume the users xp correlates with the number of triggered local analytics events:
            store.onSet = delegate {
                xpSystem.currentXp++;
                return(Task.FromResult(true));
            };

            // Set the store to be the target of the local analytics so that whenever any
            LocalAnalytics analytics = new LocalAnalytics(store);

            analytics.createStoreFor = (_) => new InMemoryKeyValueStore().GetTypeAdapter <AppFlowEvent>();

            // Setup the AppFlow logic to use LocalAnalytics:
            AppFlow.AddAppFlowTracker(new AppFlowToStore(analytics));

            // Simulate User progression by causing analytics events:
            for (int i = 0; i < eventCount; i++)
            {
                AppFlow.TrackEvent(EventConsts.catMutation, "User did mutation nr " + i);
            }

            // Get the analtics store for category "Mutations":
            var mutationStore = await analytics.GetStoreForCategory(EventConsts.catMutation).GetAll();

            Assert.Equal(eventCount, mutationStore.Count()); // All events so far were mutations
            Assert.True(eventCount <= xpSystem.currentXp);   // The user received xp for each mutation

            Assert.Equal(1000, flag4.requiredXp);            // The user needs >= 1000 xp for the feature

            // Now that the user has more than 1000 xp the condition of the TestXpSystem is met:
            Assert.True(await flag4.IsFeatureUnlocked());
            Assert.True(await FeatureFlag.IsEnabled(flagId4));
        }