Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Send view product hits

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Now that you know what the product
    actions are, you can add code to
  2. your app to track when users view
    products and add them to the cart.
  3. So for our app we are going to
    consider the activity Order Dinner
  4. as the first step in the viewing and
    shopping process.
  5. So when you create a product,
  6. you specify a product Id that
    uniquely identifies the product.
  7. But unfortunately for
    our dinners, we haven't actually
  8. given each dinner a unique Id, so
    you're going to need to do that now.
  9. So the dinner choices
    are all defined in the file,
  10. arrays.xml in values in the res folder.
  11. And each dinner is just a string.
  12. And they're grouped together according
    to whether they're vegetarian,
  13. vegan, and so on.
  14. So as a somewhat hacky, hack to quickly
    be able to get unique product IDs,
  15. we're going to add a product ID as
    the first two characters of each item.
  16. So we'll start with 01, 02, and so on.
  17. So your task is to add the prefixes
    to the dinners in arrays.xml.
  18. One consequence of our change
    though is the list of dinners
  19. is going to include the number for
    each of the dinner choices.
  20. It's kind of a cosmetic thing,
    and if you don't like it,
  21. I leave it to you to fix the code for
  22. So now each of the dinner options
    includes the Id as the first two
  23. characters.
  24. So it's going to make our life
    a lot easier if we just quickly
  25. add a utility method to get those first
    two characters to return as the Id.
  26. So go ahead and
    add getDinnerId to the utility class.
  27. Very simple method, it just gets the
    substring of the first two characters.
  28. Okay, so now we're really getting to
    the meat of thing, excuse the pun.
  29. You're going to write the code for
  30. the method that actually sends
    the ViewProductHit to analytics.
  31. So we're going to consider
    the Order Dinner screen
  32. as equivalent to the View Product.
  33. So when the user goes to the Order
    Dinner screen, they're indicating
  34. an interest in viewing and
    possibly purchasing the dinner.
  35. So, in the OrderDinnerActivity, go ahead
    and add a method sendViewProductHit.
  36. It's going to create the product action,
    which in this case is ACTION_DETAIL,
  37. because we want to indicate
    that we're viewing the product.
  38. Get the tracker,
    we all know how to do that.
  39. And send the hit as an event.
  40. The Category is Shopping steps,
    the Action,
  41. I mean these can be the things you like,
    but I used View Order Dinner screen.
  42. The Label is the selected dinner.
  43. We add the product, this is critical.
  44. And we set the ProductAction,
    and then we build the hit.
  45. Then you'll also need to make sure that
    you call sendViewProductHit() from
  46. the onStart method in
  47. So I use a Toast also to double-check,
  48. you don't have to if you don't want to.
  49. But so here's where I call
    sendViewProductHit in onStart.
  50. And you will need to make sure of course
    that you add all the imports you need.
  51. And that's Product,
    ProductAction, Tracker, and
  52. HitBuilders, and they're all in
  53. Then make sure, of course, that your
    phone is connected to Android Studio,
  54. deploy the app, run the app,
    and use the app.
  55. And make sure you list all the dinners.
  56. And you go to the Order Dinner
    screen and look for
  57. the events to come in in the real time
    section of the Analytics dashboard.
  58. And now it's the quiz.