Interface CampaignHandler

Callback implemented by the app to handle potential custom data campaigns served in response to Marketing Cloud Personalization actions generated by user interactions.

1public interface CampaignHandler

For additional information, see Mobile Data Campaigns. For lifecycle details, see Context.setCampaignHandler(com.evergage.android.CampaignHandler, java.lang.String).

Usage Details 

  • For an activity, it’s recommended to set up handlers in onStart(), as shown in this example.
  • Validate the expected campaign data or JSON before processing, since campaigns are dynamic and can be changed at any time via the Personalization UI.
  • The same campaign or content could be served again, so when applicable, use Campaign.equals(java.lang.Object) to determine if the same content is already active or visible. For example, there’s no need to re-render the same message or UI to the user if it’s already visible. This is especially true when testing a specific experience. For more information on testing, see Testing.

The following is example code that expects the featured product’s name and updates a TextView on the screen.

1// In MyActivity.java
2
3@Override
4public void onStart() {
5    super.onStart();
6
7    final Screen screen = Evergage.getInstance().getScreenForActivity(this);
8    if (screen != null) {
9        CampaignHandler handler = new CampaignHandler() {
10            @Override
11            public void handleCampaign(@NonNull Campaign campaign) {
12                // Validate the campaign data since it's dynamic JSON. Avoid processing if fails.
13                String featuredProductName = campaign.getData().optString("featuredProductName");
14                if (featuredProductName == null || featuredProductName.isEmpty()) {
15                    return;
16                }
17
18                // Check if the same content is already visible/active (see Usage Details above).
19                if (activeCampaign != null && activeCampaign.equals(campaign)) {
20                    Log.d(TAG, "Ignoring campaign name " + campaign.getCampaignName() +
21                                " since equivalent content is already active");
22                    return;
23                }
24
25                // Track the impression for statistics even if the user is in the control group.
26                screen.trackImpression(campaign);
27
28                // Only display the campaign if the user is not in the control group.
29                if (!campaign.isControlGroup()) {
30                    // Keep active campaign as long as needed for (re)display and comparison
31                    activeCampaign = campaign;
32                    Log.d(TAG, "New active campaign name " + campaign.getCampaignName() +
33                                " for target " + campaign.getTarget() + " with data " + campaign.getData());
34
35                    // Display campaign content
36                    TextView featuredProductTextView = (TextView) findViewById(R.id.featured_product_text);
37                    featuredProductTextView.setText("Our featured product is " + featuredProductName + "!");
38                }
39            }
40        };
41
42        // The target string uniquely identifies the expected data schema - here, a featured product:
43        screen.setCampaignHandler(handler, "featuredProduct");
44    }
45}

Methods 

handleCampaign 

1void handleCampaign(@NonNull Campaign campaign)

For more information and example code, see CampaignHandler

Parameters

ParameterDescription
campaignThe incoming campaign to be handled.