Interface CampaignHandler

Callback implemented by the app to handle potential custom "Data" campaigns served in response to Marketing Cloud Personalization actions generated by the user interacting with the app.

public 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 is recommended to set up handlers in onStart(), as shown in the following example.
  • Validate the expected campaign data/JSON before processing, since campaigns are dynamic and can be changed at any time via the Personalization UI.
  • The same campaign/content could be re-served, so when applicable, use Campaign.equals(java.lang.Object) to determine if the same content is already active/visible. For example, there's no need to re-render the same message/UI to the user if it's already visible. This is especially true when testing a specific experience, see Testing Guide.

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

// In MyActivity.java

@Override
public void onStart() {
    super.onStart();

    final Screen screen = Evergage.getInstance().getScreenForActivity(this);
    if (screen != null) {
        CampaignHandler handler = new CampaignHandler() {
            @Override
            public void handleCampaign(@NonNull Campaign campaign) {
                // Validate the campaign data since it's dynamic JSON. Avoid processing if fails.
                String featuredProductName = campaign.getData().optString("featuredProductName");
                if (featuredProductName == null || featuredProductName.isEmpty()) {
                    return;
                }

                // Check if the same content is already visible/active (see Usage Details above).
                if (activeCampaign != null && activeCampaign.equals(campaign)) {
                    Log.d(TAG, "Ignoring campaign name " + campaign.getCampaignName() +
                                " since equivalent content is already active");
                    return;
                }

                // Track the impression for statistics even if the user is in the control group.
                screen.trackImpression(campaign);

                // Only display the campaign if the user is not in the control group.
                if (!campaign.isControlGroup()) {
                    // Keep active campaign as long as needed for (re)display and comparison
                    activeCampaign = campaign;
                    Log.d(TAG, "New active campaign name " + campaign.getCampaignName() +
                                " for target " + campaign.getTarget() + " with data " + campaign.getData());

                    // Display campaign content
                    TextView featuredProductTextView = (TextView) findViewById(R.id.featured_product_text);
                    featuredProductTextView.setText("Our featured product is " + featuredProductName + "!");
                }
            }
        };

        // The target string uniquely identifies the expected data schema - here, a featured product:
        screen.setCampaignHandler(handler, "featuredProduct");
    }
}

Methods 

handleCampaign 

void handleCampaign(Campaign campaign)

For more information and example code, see CampaignHandler

Parameters

ParameterDescription
campaignThe incoming campaign to be handled.