Events

MyDataHelps applications can subscribe to events. Since applications may remain in memory indefinitely on iOS and Android, it is often necessary to listen for these events to know when to refresh the data being displayed.

Event Types

The following event types are currently supported:

Event Description Parameters
applicationDidBecomeVisible Triggers whenever the application becomes visible. For instance if:
  • MyDataHelps is foregrounded and this application was open.
  • The participant selects the tab for this application.
  • An application presented by this application is dismissed.
externalAccountSyncComplete Triggers whenever an external account finishes synchronizing. Only supported on iOS/Android devices. An object containing fields for externalAccountProviderID and externalAccountProviderName.
surveyDidFinish Triggers whenever a survey is finished. An object containing fields for reason and surveyName. Reasons include:
  • Completed - Participant completed the entire survey.
  • Saved - Participant exited the survey early, and saved the partial survey results for later completion.
  • Discarded - Participant exited the survey early, and did not save the partial results.
  • Failed - There was an unexpected problem loading the survey or saving the results for a completed survey.
tokenWillExpire Triggers when the participant access token is due to expire.

Subscribe to an Event

MyDataHelps.on(eventName, callback)

Triggers the callback when the specified event occurs.

Availability

MyDataHelps Views
MyDataHelps Embeddables
Web View Steps
Using surveyDidFinish to Act on Survey Results
function refreshApplicationState(result) {
  if (result.reason != "Completed") {
    return;
  }
  
  //Get the current date at midnight
  var today = new Date().setHours(0,0,0,0);

  //Query for survey answers from the MoodRating step recorded anytime today.
  var surveyQueryParams = {
    stepIdentifier: "MoodRating",
    after: today
  };

  var lowMoodThreshold = 4;

  MyDataHelps
    .querySurveyAnswers(surveyQueryParams)
    .then(function(answerPage) {

      //For simplicity, assume there is exactly 0 or 1 responses to this survey step today.
      if (answerPage.surveyAnswers[0] && 
        answerPage.surveyAnswers[0].answers[0] &&
        parseInt(answerPage.surveyAnswers[0].answers[0]) <= lowMoodThreshold) {

        //Show the participant resources to help their mood.
        MyDataHelps.showTab('MoodEnhancementResources');
      }

    });
}

MyDataHelps.on('surveyDidFinish', refreshApplicationState);
function refreshApplicationState(result) { if (result.reason != "Completed") { return; } //Get the current date at midnight var today = new Date().setHours(0,0,0,0); //Query for survey answers from the MoodRating step recorded anytime today. var surveyQueryParams = { stepIdentifier: "MoodRating", after: today }; var lowMoodThreshold = 4; MyDataHelps .querySurveyAnswers(surveyQueryParams) .then(function(answerPage) { //For simplicity, assume there is exactly 0 or 1 responses to this survey step today. if (answerPage.surveyAnswers[0] && answerPage.surveyAnswers[0].answers[0] && parseInt(answerPage.surveyAnswers[0].answers[0]) <= lowMoodThreshold) { //Show the participant resources to help their mood. MyDataHelps.showTab('MoodEnhancementResources'); } }); } MyDataHelps.on('surveyDidFinish', refreshApplicationState);

Unsubscribe from an Event

MyDataHelps.off(eventName, callback)

Removes a previously-registered event subscription.

Availability

MyDataHelps Views
MyDataHelps Embeddables
Web View Steps