processBucketing method

Campaigns processBucketing(
  1. String visitorId,
  2. Bucketing scriptBucket,
  3. Map<String, dynamic> context,
  4. Map<String, dynamic> assignHistory,
)

Implementation

Campaigns processBucketing(String visitorId, Bucketing scriptBucket,
    Map<String, dynamic> context, Map<String, dynamic> assignHistory) {
  Campaigns result =
      Campaigns(visitorId, false, [], scriptBucket.accountSettings);
  TargetingManager targetManager = TargetingManager(visitorId, context);
  // Campaign
  for (BucketCampaign itemCamp in scriptBucket.campaigns) {
    // Variation group
    for (VariationGroup itemVarGroup in itemCamp.variationGroups) {
      // Check the targeting
      if (targetManager.isTargetingGroupIsOkay(itemVarGroup.targeting) ==
          true) {
        Flagship.logger(
            Level.DEBUG,
            "The Targeting for " +
                itemVarGroup.idVariationGroup +
                "is OK 👍");

        // Check if the variationId already exist in the history before selecting by the MurMurHash
        String? varId;
        if (assignHistory.containsKey(itemVarGroup.idVariationGroup) ==
            true) {
          // The variation group already exist
          varId = assignHistory[
              itemVarGroup.idVariationGroup]; // add more security
          Flagship.logger(Level.DEBUG,
              "This variation: $varId' already selected for the visitor: $visitorId event if the allocation changed this visitor still belong to the initial bucket");
        } else {
          varId = selectIdVariationWithMurMurHash(visitorId, itemVarGroup);

          if (varId != null) {
            Flagship.logger(Level.ALL,
                "Adding a new saved variation in the assignation ");
            this
                .assignationHistory
                ?.addEntries({itemVarGroup.idVariationGroup: varId}.entries);
          }
        }

        if (varId != null) {
          // Create variation group
          Campaign camp = Campaign(
              itemCamp.idCampaign,
              itemCamp.campaignName,
              itemVarGroup.idVariationGroup,
              itemVarGroup.variationGroupName,
              itemCamp.type,
              itemCamp.slug);
          Flagship.logger(Level.DEBUG,
              "#### The variation choosen is $varId ###########");

          var matchedVariation = itemVarGroup.variations
              .firstWhereOrNull((v) => v.idVariation == varId);

          if (matchedVariation != null) {
            camp.variation = matchedVariation;

            /// Add this camp to the result
            result.campaigns.add(camp);
          } else {
            Flagship.logger(Level.DEBUG,
                "Variation $varId not found in the recent bucketing script, this variation could be removed, will return a default value ");
          }
        }
      } else {
        Flagship.logger(
            Level.DEBUG,
            "The Targeting for " +
                itemVarGroup.idVariationGroup +
                "is NOT OK 👎 ");
      }
    }
  }
  return result;
}