Audiences

Your company may use audiences to target groups of users in sending push notifications, email campaigns, or advertising campaign. If your organization has audiences enabled, you may fetch audience memberships for the current user. The result returned by the SDK is an array of audience IDs, optionally filtered by given endpoint ID.

User segment requests are done asynchronously and allow a timeout to be specified. When your app makes a user segment request, if for any reason (slow network speed, lack of network coverage, etc) the SDK does not receive a response back within the timeout period specified, the SDK will invoke the completion method with a cached version of the user segment, or with an empty user segment (if the local cache is expired or non-existent). In all cases, once a response containing user segments arrives, it will update the local cache for future requests.

The example shows a request to retrieve segments with the endpoint id “1234567890” and with a timeout of 0.5 seconds (500 milliseconds).

long timeout = 500; //500 millisecond timeout
MParticle.getInstance().getUserSegments(timeout, "1234567890", new SegmentListener() {
    @Override
    public void onSegmentsRetrieved(SegmentMembership segmentMembership) {
        //retrieve a list of the individual segments and inspect them individually
        ArrayList<Segment> segments = segmentMembership.getSegments();
        
        //SegmentMembership overrides toString() to provide a convenient comma-separated list of IDs
        String ids = segmentMembership.toString();
    }
});
val timeout: Long = 500 //500 millisecond timeout
MParticle.getInstance().getUserSegments(timeout, "1234567890") { segmentMembership ->
    //retrieve a list of the individual segments and inspect them individually
    val segments = segmentMembership.segments

    //SegmentMembership overrides toString() to provide a convenient comma-separated list of IDs
    val ids = segmentMembership.toString()
}

The SDKs provide convenience methods to concatenate all retrieved segments into a comma-separated string. This is particularly useful to add segments as tags to advertisement banners. For example, if you are using Google Mobile Ads or MoPub you could use one of the following sample codes to tag ads.

Google Mobile Ads

public class BannerExample extends Activity implements SegmentListener {
    private PublisherAdView adView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        // Create the adView
        adView = new PublisherAdView(this);
     
        long timeout = 500;
        String endpoint = "null";
        MParticle.getInstance().getUserSegments(timeout, endpoint, this);
    }
 
    @Override
    public void onSegmentsRetrieved(SegmentMembership segmentMembership) {
        //Generate the PublisherAdRequest with the SegmentMembership
        Bundle bundle = new Bundle();
        bundle.putString("MP-Segment", segmentMembership.toString());
        PublisherAdRequest request = new PublisherAdRequest.Builder()
                .addNetworkExtras(new AdMobExtras(bundle))
                .build();
 
        //Load the Ad with the PublisherAdRequest
        adView.loadAd(request);
    }
}
class BannerExample : Activity(), SegmentListener {
    private var adView : PublisherAdView? = null

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)

        // Create the adView
        adView = PublisherAdView(this)

        val timeout = 500L;
        val endpoint = "null";
        MParticle.getInstance().getUserSegments(timeout, endpoint, this);
    }

    override fun onSegmentsRetrieved(segmentMembership: SegmentMembership) {
        //Generate the PublisherAdRequest with the SegmentMembership
        val bundle = Bundle()
        bundle.putString("MP-Segment", segmentMembership.toString())
        val request = PublisherAdRequest.Builder()
                .addNetworkExtras(AdMobExtras(bundle))
                .build();

        //Load the Ad with the PublisherAdRequest
        adView.loadAd(request);
    }
}

MoPub

import com.mparticle.MParticle;
import com.mparticle.segmentation.*;
 
public class BannerExample extends Activity implements SegmentListener {
    private MoPubView moPubView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //contains a <com.mopub.mobileads.MoPubView/> definition
 
        // Retrieve a reference to the MoPubView
        moPubView = (MoPubView) findViewById(R.id.adview);
        moPubView.setAdUnitId("xxxxxxxxxxx"); // Enter your Ad Unit ID from www.mopub.com
     
        long timeout = 500;
        String endpoint = "%reference_key%"; 
        MParticle.getInstance().getUserSegments(timeout, endpoint, this);
    }
 
    @Override
    public void onSegmentsRetrieved(SegmentMembership segmentMembership) {
        //Add mParticle SegmentMembership as comma-separated keywords
        StringBuilder keywords = new StringBuilder();
        for (Segment segment : segmentMembership.getSegments()) {
            keywords.append("MP-Segment:");
            keywords.append(segment.getId());
            keywords.append(",");
        }
        keywords.deleteCharAt(keywords.length()-1); //delete the trailing comma
        
        moPubView.setKeywords(keywords.toString());
        moPubView.loadAd();
    }
}
import com.mparticle.MParticle;
import com.mparticle.segmentation.*;

class BannerExample : Activity(), SegmentListener {
    private var moPubView : MoPubView? = null

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main) //contains a <com.mopub.mobileads.MoPubView/> definition

        // Retrieve a reference to the MoPubView
        moPubView = findViewById(R.id.adview)
        moPubView.setAdUnitId("xxxxxxxxxxx") // Enter your Ad Unit ID from www.mopub.com

        val timeout = 500L
        val endpoint = "%reference_key%"
        MParticle.getInstance().getUserSegments(timeout, endpoint, this)
    }


    override fun onSegmentsRetrieved(segmentMembership: SegmentMembership) {
        //Add mParticle SegmentMembership as comma-separated keywords
        val keywords = StringBuilder()
        segmentMembership.getSegments().forEach { segment ->
            keywords.append("MP-Segment:");
            keywords.append(segment.getId());
            keywords.append(",");
        }
        keywords.deleteCharAt(keywords.length - 1); //delete the trailing comma

        moPubView.setKeywords(keywords.toString());
        moPubView.loadAd();
    }
}