2121import com .vwo .enums .ApiEnum ;
2222import com .vwo .enums .CampaignTypeEnum ;
2323import com .vwo .enums .DebuggerCategoryEnum ;
24+ import com .vwo .enums .EventEnum ;
2425import com .vwo .constants .Constants ;
2526import com .vwo .models .*;
27+ import com .vwo .models .request .EventArchPayload ;
2628import com .vwo .models .user .GetFlag ;
2729import com .vwo .models .user .VWOContext ;
2830import com .vwo .packages .logger .enums .LogLevelEnum ;
2931import com .vwo .services .StorageService ;
3032import com .vwo .utils .DebuggerServiceUtil ;
33+ import com .vwo .utils .NetworkUtil ;
3134import com .vwo .utils .RuleEvaluationUtil ;
3235
3336import java .util .*;
3437
3538import static com .vwo .utils .CampaignUtil .getVariationFromCampaignKey ;
3639import static com .vwo .utils .DecisionUtil .evaluateTrafficAndGetVariation ;
3740import static com .vwo .utils .FunctionUtil .*;
38- import static com .vwo .utils .ImpressionUtil .createAndSendImpressionForVariationShown ;
41+ import static com .vwo .utils .ImpressionUtil .sendImpressionForVariationShown ;
42+ import static com .vwo .utils .ImpressionUtil .sendImpressionForVariationShownInBatch ;
3943
4044public class GetFlagAPI {
4145
@@ -52,6 +56,7 @@ public static GetFlag getFlag(String featureKey, VWOContext context, ServiceCont
5256
5357 Map <String , Object > passedRulesInformation = new HashMap <>();
5458 Map <String , Object > evaluatedFeatureMap = new HashMap <>();
59+ List <EventArchPayload > batchPayloads = new ArrayList <>();
5560
5661 // get feature object from feature key
5762 Feature feature = getFeatureFromKey (serviceContainer .getSettings (), featureKey );
@@ -184,7 +189,24 @@ public static GetFlag getFlag(String featureKey, VWOContext context, ServiceCont
184189 getFlag .setVariables (variation .getVariables ());
185190 shouldCheckForExperimentsRules = true ;
186191 updateIntegrationsDecisionObject (passedRolloutCampaign , variation , passedRulesInformation , decision );
187- createAndSendImpressionForVariationShown (serviceContainer , passedRolloutCampaign .getId (), variation .getId (), context );
192+
193+ // Create payload for sending
194+ EventArchPayload payload = NetworkUtil .getTrackUserPayloadData (
195+ serviceContainer ,
196+ context .getId (),
197+ EventEnum .VWO_VARIATION_SHOWN .getValue (),
198+ passedRolloutCampaign .getId (),
199+ variation .getId (),
200+ context .getUserAgent (),
201+ context .getIpAddress ()
202+ );
203+ if (serviceContainer .getSettingsManager ().isGatewayServiceProvided && payload != null ) {
204+ // Gateway service: send immediately
205+ sendImpressionForVariationShown (serviceContainer , passedRolloutCampaign .getId (), variation .getId (), context , payload );
206+ } else if (payload != null ) {
207+ // Non-gateway: add to batch
208+ batchPayloads .add (payload );
209+ }
188210 }
189211 }
190212 } else if (!shouldCheckForExperimentsRules ) {
@@ -212,12 +234,24 @@ public static GetFlag getFlag(String featureKey, VWOContext context, ServiceCont
212234 if (whitelistedObject == null ) {
213235 experimentRulesToEvaluate .add (rule );
214236 } else {
215- // If whitelisted object is not null, update the decision object and send an impression
237+ // If whitelisted object is not null, update the decision object and handle payload
216238 getFlag .setIsEnabled (true );
217239 getFlag .setVariables (whitelistedObject .getVariables ());
218240 passedRulesInformation .put ("experimentId" , rule .getId ());
219241 passedRulesInformation .put ("experimentKey" , rule .getKey ());
220242 passedRulesInformation .put ("experimentVariationId" , whitelistedObject .getId ());
243+
244+ // Handle whitelisting payload
245+ EventArchPayload whitelistPayload = (EventArchPayload ) evaluateRuleResult .get ("payload" );
246+ if (whitelistPayload != null ) {
247+ if (serviceContainer .getSettingsManager ().isGatewayServiceProvided ) {
248+ // Gateway service: send immediately
249+ sendImpressionForVariationShown (serviceContainer , rule .getId (), whitelistedObject .getId (), context , whitelistPayload );
250+ } else {
251+ // Non-gateway: add to batch
252+ batchPayloads .add (whitelistPayload );
253+ }
254+ }
221255 }
222256 break ;
223257 }
@@ -231,7 +265,24 @@ public static GetFlag getFlag(String featureKey, VWOContext context, ServiceCont
231265 getFlag .setIsEnabled (true );
232266 getFlag .setVariables (variation .getVariables ());
233267 updateIntegrationsDecisionObject (campaign , variation , passedRulesInformation , decision );
234- createAndSendImpressionForVariationShown (serviceContainer , campaign .getId (), variation .getId (), context );
268+
269+ // Create payload for sending
270+ EventArchPayload payload = NetworkUtil .getTrackUserPayloadData (
271+ serviceContainer ,
272+ context .getId (),
273+ EventEnum .VWO_VARIATION_SHOWN .getValue (),
274+ campaign .getId (),
275+ variation .getId (),
276+ context .getUserAgent (),
277+ context .getIpAddress ()
278+ );
279+ if (serviceContainer .getSettingsManager ().isGatewayServiceProvided && payload != null ) {
280+ // Gateway service: send immediately
281+ sendImpressionForVariationShown (serviceContainer , campaign .getId (), variation .getId (), context , payload );
282+ } else if (payload != null ) {
283+ // Non-gateway: add to batch
284+ batchPayloads .add (payload );
285+ }
235286 }
236287 }
237288 }
@@ -266,13 +317,37 @@ public static GetFlag getFlag(String featureKey, VWOContext context, ServiceCont
266317 put ("status" , getFlag .isEnabled () ? "enabled" : "disabled" );
267318 }
268319 });
269- createAndSendImpressionForVariationShown (
320+
321+ // Create payload for impact campaign
322+ EventArchPayload impactPayload = NetworkUtil .getTrackUserPayloadData (
270323 serviceContainer ,
324+ context .getId (),
325+ EventEnum .VWO_VARIATION_SHOWN .getValue (),
271326 feature .getImpactCampaign ().getCampaignId (),
272327 getFlag .isEnabled () ? 2 : 1 ,
273- context
328+ context .getUserAgent (),
329+ context .getIpAddress ()
274330 );
331+ if (serviceContainer .getSettingsManager ().isGatewayServiceProvided && impactPayload != null ) {
332+ // Gateway service: send immediately
333+ sendImpressionForVariationShown (
334+ serviceContainer ,
335+ feature .getImpactCampaign ().getCampaignId (),
336+ getFlag .isEnabled () ? 2 : 1 ,
337+ context ,
338+ impactPayload
339+ );
340+ } else if (impactPayload != null ) {
341+ // Non-gateway: add to batch
342+ batchPayloads .add (impactPayload );
343+ }
275344 }
345+
346+ // Send all collected payloads in a single batch request
347+ if (!batchPayloads .isEmpty () && !serviceContainer .getSettingsManager ().isGatewayServiceProvided ) {
348+ sendImpressionForVariationShownInBatch (batchPayloads , serviceContainer );
349+ }
350+
276351 return getFlag ;
277352 }
278353
0 commit comments