Skip to content

Instantly share code, notes, and snippets.

@zuzannamj
Last active September 29, 2020 14:05
Show Gist options
  • Save zuzannamj/e45c77597f7992e4e10316c09076f850 to your computer and use it in GitHub Desktop.
Save zuzannamj/e45c77597f7992e4e10316c09076f850 to your computer and use it in GitHub Desktop.
<script runat="server">
Platform.Load("Core", "1");
try {
//authenticate to get access token
var authEndpoint = "https://mcxxxxxxxxxx.auth.marketingcloudapis.com/"; //provide auth endpoint
var payload = {
client_id: "xxxxxx", //pass client id
client_secret: "xxxxxx", //pass client secret
grant_type: "client_credentials",
account_id: "12345" //pass BU's MID
};
var url = authEndpoint + "/v2/token";
var contentType = "application/json";
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if (accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
var accessToken = tokenResponse.access_token
var rest_instance_url = tokenResponse.rest_instance_url
};
//make api call to get journey details
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var payload = '';
var requestUrl = rest_instance_url + "interaction/v1/interactions/journeyhistory/search?$page=1&$pageSize=6000";
var getJourneyHistory = HTTP.Post(requestUrl, contentType, payload, headerNames, headerValues);
var response = Platform.Function.ParseJSON(getJourneyHistory.Response[0]);
var items = response.items;
//group by contact key and journey
var keys = ["definitionId", "contactKey"],
temp = {},
endResult = [],
i, j, k, key;
for (i = 0; i < items.length; i++) {
key = '';
for (j = 0; j < keys.length; j++) {
key += items[i][keys[j]] + '|';
}
if (temp[key] === undefined) temp[key] = [];
temp[key].push({
definitionId: items[i].definitionId,
contactKey: items[i].contactKey,
status: items[i].status,
definitionName: items[i].definitionName
});
}
//filter out contacts that failed or completed
for (k in temp) {
if (Stringify(temp[k]).indexOf("Failed") === -1 && Stringify(temp[k]).indexOf("Complete") === -1) {
endResult.push({
contactKey: temp[k][0].contactKey,
definitionName: temp[k][0].definitionName
});
}
}
Write(Stringify(endResult));
} catch (error) {
Write(Stringify(error));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment