public MessagesAdapter(
     Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
   super(context, layout, c, from, to, flags);
   this.context = context;
   mayday = new Mayday(context);
   mixpanel = MixpanelAPI.getInstance(context, Constants.MIXPANEL_TOKEN);
   preferences = context.getSharedPreferences("boutlineData", Context.MODE_PRIVATE);
   String userId = preferences.getString("boutlineUserId", "");
   mixpanel.identify(userId);
 }
  public void testAlias() {
    final ServerMessage mockPoster =
        new ServerMessage() {
          @Override
          public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePairs) {
            try {
              assertEquals(nameValuePairs.get(0).getName(), "data");
              final String jsonData = Base64Coder.decodeString(nameValuePairs.get(0).getValue());
              JSONArray msg = new JSONArray(jsonData);
              JSONObject event = msg.getJSONObject(0);
              JSONObject properties = event.getJSONObject("properties");

              assertEquals(event.getString("event"), "$create_alias");
              assertEquals(properties.getString("distinct_id"), "old id");
              assertEquals(properties.getString("alias"), "new id");
            } catch (JSONException e) {
              throw new RuntimeException("Malformed data passed to test mock", e);
            }
            return TestUtils.bytes("1\n");
          }
        };

    final AnalyticsMessages listener =
        new AnalyticsMessages(getContext()) {
          @Override
          protected ServerMessage getPoster() {
            return mockPoster;
          }
        };

    MixpanelAPI metrics =
        new TestUtils.CleanMixpanelAPI(getContext(), mMockPreferences, "Test Message Queuing") {
          @Override
          protected AnalyticsMessages getAnalyticsMessages() {
            return listener;
          }
        };

    // Check that we post the alias immediately
    metrics.identify("old id");
    metrics.alias("new id", "old id");
  }
  public void testIdentifyAndGetDistinctId() {
    MixpanelAPI metrics =
        new TestUtils.CleanMixpanelAPI(getContext(), mMockPreferences, "Identify Test Token");

    String generatedId = metrics.getDistinctId();
    assertNotNull(generatedId);

    String emptyId = metrics.getPeople().getDistinctId();
    assertNull(emptyId);

    metrics.identify("Events Id");
    String setId = metrics.getDistinctId();
    assertEquals("Events Id", setId);

    String stillEmpty = metrics.getPeople().getDistinctId();
    assertNull(stillEmpty);

    metrics.getPeople().identify("People Id");
    String unchangedId = metrics.getDistinctId();
    assertEquals("Events Id", unchangedId);

    String setPeopleId = metrics.getPeople().getDistinctId();
    assertEquals("People Id", setPeopleId);
  }
 /**
  * Sets a user ID to identify events by.
  *
  * @param id
  */
 @ReactMethod
 public void identify(String id) {
   mixpanel.identify(id);
   mixpanel.getPeople().identify(id);
 }
  public void testPersistence() {
    MixpanelAPI metricsOne = new MixpanelAPI(getContext(), mMockPreferences, "SAME TOKEN");
    metricsOne.clearPreferences();

    JSONObject props;
    try {
      props = new JSONObject("{ 'a' : 'value of a', 'b' : 'value of b' }");
    } catch (JSONException e) {
      throw new RuntimeException("Can't construct fixture for super properties test.");
    }

    metricsOne.clearSuperProperties();
    metricsOne.registerSuperProperties(props);
    metricsOne.identify("Expected Events Identity");
    metricsOne.getPeople().identify("Expected People Identity");

    // We exploit the fact that any metrics object with the same token
    // will get their values from the same persistent store.

    final List<Object> messages = new ArrayList<Object>();
    final AnalyticsMessages listener =
        new AnalyticsMessages(getContext()) {
          @Override
          public void eventsMessage(EventDescription heard) {
            messages.add(heard);
          }

          @Override
          public void peopleMessage(JSONObject heard) {
            messages.add(heard);
          }
        };

    class ListeningAPI extends MixpanelAPI {
      public ListeningAPI(Context c, Future<SharedPreferences> prefs, String token) {
        super(c, prefs, token);
      }

      @Override
      protected AnalyticsMessages getAnalyticsMessages() {
        return listener;
      }
    }

    MixpanelAPI differentToken =
        new ListeningAPI(getContext(), mMockPreferences, "DIFFERENT TOKEN");

    differentToken.track("other event", null);
    differentToken.getPeople().set("other people prop", "Word"); // should be queued up.

    assertEquals(1, messages.size());

    AnalyticsMessages.EventDescription eventMessage =
        (AnalyticsMessages.EventDescription) messages.get(0);

    try {
      JSONObject eventProps = eventMessage.getProperties();
      String sentId = eventProps.getString("distinct_id");
      String sentA = eventProps.optString("a");
      String sentB = eventProps.optString("b");

      assertFalse("Expected Events Identity".equals(sentId));
      assertEquals("", sentA);
      assertEquals("", sentB);
    } catch (JSONException e) {
      fail("Event message has an unexpected shape " + e);
    }

    messages.clear();

    MixpanelAPI metricsTwo = new ListeningAPI(getContext(), mMockPreferences, "SAME TOKEN");

    metricsTwo.track("eventname", null);
    metricsTwo.getPeople().set("people prop name", "Indeed");

    assertEquals(2, messages.size());

    eventMessage = (AnalyticsMessages.EventDescription) messages.get(0);
    JSONObject peopleMessage = (JSONObject) messages.get(1);

    try {
      JSONObject eventProps = eventMessage.getProperties();
      String sentId = eventProps.getString("distinct_id");
      String sentA = eventProps.getString("a");
      String sentB = eventProps.getString("b");

      assertEquals("Expected Events Identity", sentId);
      assertEquals("value of a", sentA);
      assertEquals("value of b", sentB);
    } catch (JSONException e) {
      fail("Event message has an unexpected shape " + e);
    }

    try {
      String sentId = peopleMessage.getString("$distinct_id");
      assertEquals("Expected People Identity", sentId);
    } catch (JSONException e) {
      fail("Event message has an unexpected shape: " + peopleMessage.toString());
    }
  }