Automate Your Journey Management with SSJS
In the realm of digital marketing, efficiency is key. If you've ever faced the challenge of removing contacts from multiple journeys or executing bulk removals from the same journey in Marketing Cloud, then you understand the importance of automation.
Today, we're diving into the world of Server-Side JavaScript (SSJS) to streamline your journey management processes. By leveraging the power of SSJS, you can automate these tasks and free up valuable time for strategic initiatives.
Let's take a closer look at how you can achieve this automation:
Obtaining Access Token: Start by obtaining the necessary access token using your client ID and client secret. This token will authenticate your requests to Marketing Cloud's APIs.
Making REST API Calls: With the access token in hand, you can construct REST API calls to interact with Marketing Cloud. This includes specifying the contact keys, definition keys, and versions for the actions you want to perform.
Executing Actions: Once your API calls are set up, execute them using SSJS. This will trigger the desired actions, such as removing contacts from journeys or performing bulk removals.
<script runat="server">
Platform.Load("core", "1");
function getAccessToken() {
var clientId = 'xxxxxxxxxxxxx'; // Provide client Id
var clientSecret = 'xxxxxxxxxxxxxxx'; // Provide client secret
var url = 'https://TenantEndPoint.auth.marketingcloudapis.com/';
var contentType = 'application/json';
var payload = {
"client_id": clientId,
"client_secret": clientSecret,
"grant_type": "client_credentials"
};
try {
var accessTokenResult = HTTP.Post(url, contentType, Stringify(payload));
var statusCode = accessTokenResult["StatusCode"];
var response = accessTokenResult["Response"][0];
var accessToken = Platform.Function.ParseJSON(response).access_token;
//Variable.SetValue("@accessToken", accessToken);
return accessToken;
} catch (e) {
Write("Error getting access token: " + e.message);
return null;
}
}
function makeRESTCall(accessToken) {
try {
var setupURL = 'https://TenantEndPoint.rest.marketingcloudapis.com/interaction/v1/interactions/contactexit';
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var payload = [
{
"ContactKey": "xxxxxxxxxx",
"DefinitionKey": "xxxxxxxxxxxxxxxx",
"Versions": [2]
},
{
"ContactKey": "xxxxxxxxxx",
"DefinitionKey": "xxxxxxxxxxxxxxxx",
},
{
"ContactKey": "xxxxxxxxxx",
"DefinitionKey": "xxxxxxxxxxxxxxxx",
"Versions": [5]
}
];
var restResponse = HTTP.Post(setupURL, contentType, Stringify(payload), headerNames, headerValues);
Write(restResponse.Response);
} catch (e) {
Write("Error making REST call: " + e.message);
}
}
try {
var accessToken = getAccessToken();
if (accessToken) {
makeRESTCall(accessToken);
} else {
Write("Access token not obtained.");
}
} catch (e) {
Write("General error: " + e.message);
}
</script>
By incorporating SSJS into your workflow, you not only save time but also ensure accuracy and consistency in your journey management tasks.