Skip to main content

MeDataService

Script-engine API for reading MeData definitions/categories and for saving or collecting MeData records. Available in Journey Script steps, Rule script actions, and Script-sourced MeData Definitions.

Functions

saveMeDataRecord

MeDataService.saveMeDataRecord(name, values): Promise<void>

Saves a new MeData record for the given definition name with the provided values.

Parameters:

  • name: string — MeData definition name (for example "daily_steps").
  • values: any — JSON‑serializable value or array of values to be stored for the definition.

getMeDataDefinitions

MeDataService.getMeDataDefinitions(): Promise<MeDataDefinition[]>

Returns all MeData definitions as a JSON array.

Parameters: None.

getMeDataDefinition

MeDataService.getMeDataDefinition(name): Promise<MeDataDefinition | null>

Returns a single MeData definition by its name, or null if it is not found.

Parameters:

  • name: string — The unique MeData definition name (for example "daily_steps").

getMeDataCategories

MeDataService.getMeDataCategories(): Promise<MeDataCategory[]>

Returns all MeData categories as an array, or null if no categories are available.

Parameters: None.

getMeDataDefinitionsByCategory

MeDataService.getMeDataDefinitionsByCategory(name): Promise<MeDataDefinition[]>

Returns MeData definitions for the given category name, or null if no definitions exist in that category.

Parameters:

  • name: string — Category name to filter MeData definitions.

getMeDataRecords

MeDataService.getMeDataRecords(name): Promise<MeDataRecord[]>

Returns all stored records for the given MeData definition name, or null if no records exist.

Parameters:

  • name: string — MeData definition name whose records should be fetched.

getLastMeDataRecord

MeDataService.getLastMeDataRecord(name): Promise<MeDataRecord | null>

Returns the most recent MeData record for the given definition name, or null if there is no record.

Parameters:

  • name: string — MeData definition name whose latest record should be returned.

collectMeData

MeDataService.collectMeData(meDataDefinitionNames): Promise<Map<string, any>>

Triggers MeData collectors for the given definition names and returns an object mapping each definition name to its collected data.

Parameters:

  • meDataDefinitionNames: string[] — Array of MeData definition names to collect.

getAggregatedMeDataRecord

MeDataService.getAggregatedMeDataRecord(
name,
startDate,
endDate,
aggregation,
aggregationInterval
): Promise<AggregatedMeDataRecord>

Aggregates the numeric MeData records in the given date range into a single record. When aggregationInterval is provided, values are first reduced per interval window using the interval's own aggregation, and aggregation is then applied over the interval results. A record is always returned; its value is null when the range holds no numeric data.

Parameters:

  • name: string — MeData definition name. The definition must be of NUMBER data type.
  • startDate: number — Start of the range, epoch milliseconds.
  • endDate: number — End of the range, epoch milliseconds.
  • aggregation: Aggregation — Aggregation applied over the range: "SUM", "MIN", "MAX" or "AVG".
  • aggregationInterval: AggregationInterval | null — Optional windowing, for example { seconds: 86400, aggregation: "SUM" }. seconds must be greater than 0.
const record = await MeDataService.getAggregatedMeDataRecord(
"step_count",
Date.now() - 7 * 86400000,
Date.now(),
"AVG",
{ seconds: 86400, aggregation: "SUM" }
);
console.log("average daily steps: " + record.value);

getAggregatedMeDataRecordIntervals

MeDataService.getAggregatedMeDataRecordIntervals(
name,
startDate,
endDate,
aggregationInterval
): Promise<AggregatedMeDataRecord[]>

Splits the given date range into windows of aggregationInterval.seconds and returns one aggregated record per window. Every window in the range is present in the result, in ascending order; windows that hold no numeric record carry a null value.

Parameters:

  • name: string — MeData definition name. The definition must be of NUMBER data type.
  • startDate: number — Start of the range, epoch milliseconds.
  • endDate: number — End of the range, epoch milliseconds.
  • aggregationInterval: AggregationInterval — Window length and the aggregation applied inside each window, for example { seconds: 86400, aggregation: "SUM" }. seconds must be greater than 0.
const records = await MeDataService.getAggregatedMeDataRecordIntervals(
"step_count",
Date.now() - 7 * 86400000,
Date.now(),
{ seconds: 86400, aggregation: "SUM" }
);
console.log("daily totals: " + JSON.stringify(records));

Classes

MeDataDefinition

{
id: string;
name: string;
text: string;
source: string;
script?: Script | null;
valueDef?: ValueDefinition | null;
category?: MeDataCategory | null;
iconUrl?: string | null;
description?: string | null;
sharable?: boolean | null;
hidden?: boolean | null;
storageSettings?: MeDataStorageSettings | null;
question?: Question | null;
}

MeDataStorageSettings

{
onlyIfChanged?: boolean | null;
longLimit?: number | null;
}

DataType

"STRING" | "NUMBER" | "BOOLEAN" | "DATETIME" | "IMAGE" | "VIDEO" | "OBJECT" | "UNKNOWN" | null

EnumeratedValue

{
id: string;
name: string;
text: string;
description?: string | null;
imageUrl?: string | null;
}

EnumeratedValueConstraints

{
allowNone?: boolean | null;
allowIdk?: boolean | null;
allowNa?: boolean | null;
allowOther?: boolean | null;
}

MeDataCategory

{
id: string;
text: string;
name: string;
iconUrl?: string | null;
description?: string | null;
}

MeDataType

"NATIVE" | "QA" | "SCRIPT" | "INFERRED" | "UNKNOWN"

Question

{
id: string;
name: string;
text: string;
description?: string | null;
imageUrl?: string | null;
}

QuestionDefinition

{
valueDef: ValueDefinition;
question: Question;
isMeData: boolean;
contextKey: string;
}

ValueDefinition

{
type: string;
constraints?: ValueTypeConstraints | null;
multivalued?: boolean | null;
multiValueConstraints?: MultiValueConstraints | null;
enumerated?: boolean | null;
enumeratedValues?: EnumeratedValue[] | null;
enumeratedValueConstraints?: EnumeratedValueConstraints | null;
}

ValueTypeConstraints

{
minValue?: number | null;
maxValue?: number | null;
regex?: string | null;
allowCamera?: boolean | null;
allowGallery?: boolean | null;
}

MultiValueConstraints

{
minValue: number;
maxValue: number;
}

MeData

{
id: string;
definitionName: string;
records: MeDataRecord[];
}

MeDataRecord

{
id: string;
meDataDefinitionId: string;
notAvailable: boolean;
date: number;
values: MeDataValue[];
}

MeDataValue

{
id: string;
evName?: string | null;
value: string;
}

AggregatedMeDataRecord

{
startDate: number; // epoch milliseconds
endDate: number; // epoch milliseconds
value: number | null; // null = no data in the range/window, not a measured 0
}

AggregationInterval

{
seconds: number; // window length, must be greater than 0
aggregation: Aggregation; // applied inside each window
}

Aggregation

"SUM" | "MIN" | "MAX" | "AVG"