Lesson5: Real Time Targeting - Adobe-Marketing-Cloud/target-iot-lab GitHub Wiki

##Objective

In this lesson, we will how a Mobile app can be used both for delivering a personalized experience and also to influence the visitor profile using a real time signal. Note that in the real world we can also pick this signal up from any IoT device such as a FitBit.

Our objective in this lesson is to review an existing mobile project, and have it run on a emulator on your lab machines. The mobile app is preconfigured to make a mbox request for "wellnessMobile" mbox.

We will update the "LabUser1 - Wellness Activity" to also send a specific experience for the mobile mbox.

##Exercise 1: Update the Wellness Activity.

  • Step 1: Login to target and locate the activity that you created and updated in previous lessons.

  • Step 2: Edit the activity as follows:

Add a new location for "wellnessMobile"

Next, click "Add Refinements" below the wellnessMobile location you just added, and specify targeting condition for mbox parameter "name" equals your LabUserID as shown

Next, for each experience select the offer for the mobile experience as follows:

  • WellnessActiveStateUnknown audience should be shown Mobile_Default offer

  • WellnessCouchPotato should be shown Mobile_CouchPotato offer

  • WellnessFitnessFreak should be shown Mobile_FitnessFreak offer

  • WellnessMarathoner should be shown Mobile_Marathoner offer

  • Step 3: Click Next after you have added the new offers for the wellnessMobile mbox , for each experience

  • Step 4: Click Next again on the overview of the activity, no changes are needed.

  • Step 5: Click Save, without making any changes to the conversion step.

  • Step 6: Lets test the changes by having a mobile application call this mbox!

##Exercise 2: Run Wellness Mobile application.

  • Step 1: You should already have Android studio and the wellness_mobile project open. If not, launch it.

  • Step 2: Click on the green arrow and then hit OK when the device window pops up.

You should see that the mobile emulator has popped up.

  • Step 3: Click again on the green arrow and then hit OK when the device window pops up.

You should see that our app now appears in the mobile emulator. This may take a few seconds. If it is not working, try hitting the green run button again.

  • Step 4: We have setup the app to make an mbox call back to Target. If you'd like, you can take a look back in Android studio to see the mbox request. To to this, locate the class that makes the mbox request by going here: .../tnt-auth-mobile/app/src/main/java/summit/adobe/com/summitauthmboxapp/TNTRequestService.java

Otherwise, we have pasted the request here for you to see.

  public String getContent(String mbox, Map<String, String> mboxParameters,
                           Map<String, String> profileParameters) throws TntApiCallException {
    String host = StringUtils.defaultString(edgeHost, clientCode + ".tt.omtrdc.net");
    String url = "http://" + host + "/rest/v1/mbox/" + thirdPartyId +
      "?client=" + clientCode;
    try {
      URL urlToRequest = new URL(url);
      HttpURLConnection urlConnection = (HttpURLConnection) urlToRequest.openConnection();
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("POST");
      urlConnection.setRequestProperty("Content-Type", "application/json");

      JSONObject mboxRequestJson = new JSONObject();
      mboxRequestJson.put("mbox", mbox);
      mboxRequestJson.put("thirdPartyId", thirdPartyId);
      mboxRequestJson.put("mboxParameters", new JSONObject(mboxParameters));
      mboxRequestJson.put("profileParameters", new JSONObject(profileParameters));
      DataOutputStream dStream = new DataOutputStream(urlConnection.getOutputStream());
      dStream.writeBytes(mboxRequestJson.toString());
      dStream.flush();
      dStream.close();
      activity.debug("POST to " + url + " " + mboxRequestJson.toString());
  • Step 5: Now, go back to your emulator. You will see a default view, with a form to enter the Lab User name and profile id. Enter Lab User Name, and Profile User Id to match with your tests before and hit submit.

You will start see messages in the debug console about the request being sent to the Target server, and the response.

  • Step 7: We have setup this app's experience (in this case, image) change in real-time in response to motion. When you move your mouse over the app, the active state of that user changes, gets updated in Target, and a new experience is shown. You could similarly connect up your Fitbit and have an experience get updated in real time based on Fitbit data.

Move your mouse over the form in the mobile app and test this out. You will see the activeState move from couchPotato to fitnessfreak and then to marathoner in the debug window.

  • Step 8: Now, what if you wanted to impact the experience on another channel based on the "motion" in the mobile app? Try it out. Open up your light bulb application and you will see the light bulb experience also change in real time.

  • Step 8 : Look at the mobile app project once more. Note that the mobile app also calls the "Profile update" api each time the activeState changes.
  public void updateProfile(Map<String, String> profileParameters) throws TntApiCallException {
    if (profileParameters == null || profileParameters.size() == 0) {
      activity.debug("Not making profile update call to Target since profileParameters is empty");
      return;
    }

    StringBuilder parametersString = new StringBuilder();
    for (Map.Entry<String, String> parameter : profileParameters.entrySet()) {
      parametersString.append("&").append("profile.").append(parameter.getKey()).append("=").append(parameter.getValue());
    }

    String host = StringUtils.defaultString(edgeHost, clientCode + ".tt.omtrdc.net");
    String url = "http://" + host + "/m2/adobesummit021/profile/update?mbox3rdPartyId=" + thirdPartyId + parametersString;
    try {
      URL urlToRequest = new URL(url);
      HttpURLConnection urlConnection = (HttpURLConnection) urlToRequest.openConnection();
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("GET");

      activity.debug("GET to " + url);

      int responseCode = urlConnection.getResponseCode();
      if (responseCode != 200) {
        StringWriter writer = new StringWriter();
        IOUtils.copy(urlConnection.getErrorStream(), writer);
        throw new TntApiCallException(responseCode + " " + writer.toString());
      }

This call causes Target to update all active sessions for the same thirdpartyId. This means that even the Web Application will pick up the profile update even though it is running its own session. Try to test this out by going to the Wellness Website and hitting Ok, you would be control the experience shown in the web application by moving the mouse on the Mobile App !