Bạn có biết một vấn đề sau khi dùng Journey trong Marketing Cloud một thời gian là gì không? Đó chính là có sẽ có nhiều journey không còn dùng nữa, những test journey chưa tắt,… làm cho hệ thống của bạn cồng kềnh, chậm và dễ gây nhầm lẫn cho người sử dụng.
Thật may là nhờ có dữ liệu thống kê ở bài trước, phòng quản lý kinh doanh đã quyết định chọn lọc và muốn bạn dọn dẹp những journey không còn dùng đến.
Vậy làm sao để dừng các journey này đây? 2, 3 cái thì vào UI làm cho nhanh, nhưng ở đây có đến hàng trăm cái, làm bạn muốn xỉu up xỉu down.
Đừng xỉu, vì hôm nay Đần sẽ chỉ các bạn cách để dừng hàng loạt các journey nhé.

Tình huống:
Như đã nói từ đầu bài, từ dữ liệu thống kê đã làm mà ta đã xác định được journey nào nên giữ lại/ journey nào bỏ.
- Giờ ta sẽ import dữ liệu này lên một data extension, đặt tên là Journeys to be deactivated.
- Data này ít nhất phải có các field là JourneyID, VersionNumber, remark (xem lại bài trước nhé), và nên có JourneyName để kiểm tra qua UI.
Để dừng hàng loạt journey, Đần sẽ dùng API được ghi ở ĐÂY. Các bạn xem sơ qua cũng thấy, REST API này cần có parameter là id, versionNumber của journey và cần Authorization bằng access token nữa.
Vậy ta cần kết hợp giữa:
- Lấy access token (tham khảo Lấy access token từ Salesforce Marketing Cloud)
- Dữ liệu journey sau khi được chọn lọc ( tham khảo Thống kê Journey trong Marketing Cloud)
Ý tưởng
Viết một function stop_journey dừng journey, sau đi retrieve dữ liệu từ DE Journeys to be deactivated, chạy vòng lặp và truyền vào các thông số cho đến khi xử lý hết.
Viết function dừng journey dựa trên REST API ở trên
<script runat="server">
Platform.Load("Core","1.1.1");
var debugging = true;
function stop_journey(journeyid, version, accesstoken, debugging) {
/*
journeyid : id of the journey
version: version of the journey
access token: token was called from above
debugging: debug mode
*/
var endpoint = 'https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/interaction/v1/interactions/stop/' + journeyid + '?versionNumber=' + version;
var req = new Script.Util.HttpRequest(endpoint);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = 'application/json';
req.method = 'POST';
var auth = 'Bearer ' + accesstoken;
req.setHeader('Authorization', auth);
var resp = req.send();
var resultJSON = Platform.Function.ParseJSON(String(resp.content));
if (debugging) {
Write(Stringify(resultJSON) + '<br>');
};
}
</script>
Combine lấy access token, retrieve dữ liệu từ DE và chạy function xử lý
Final code:
<script runat="server">
Platform.Load("Core","1.1.1");
var debugging = true;
var now = new Date();
var time = now.getTime(); // milliseconds
var accesstoken;
/*
Validation to check whether the access token is expired or not
- If the DE is empty then call api to create and store a new token
- If the DE is having a access token then check the expiry
token is expired -> create a new token
token is still usable -> keep using old token
*/
var DEname = 'Token response'; // DE stores access token
var data = Platform.Function.LookupRows(DEname,['remark'], ['my token']);
if (data != null && (data[0].time_out > time)) {
Write('Using old token.<br>');
accesstoken = data[0].access_token;
} else {
Write('Creating a new token.<br>');
accesstoken = get_token(DEname, time, debugging);
};
// This part for calling api and stopping journey
var journeyDEname = 'Journeys to be deactivated';
var journeyData = Platform.Function.LookupRows(journeyDEname,['remark'], ['journey']);
// Create a validation here
if (journeyData !=null && (journeyData.length > 0) ) {
for (var i=0; i < journeyData.length; i++) {
var journeyid = journeyData[i].JourneyID ;
var version = journeyData[i].VersionNumber;
stop_journey(journeyid, version, accesstoken, debugging); // stop journey
}
};
function stop_journey(journeyid, version, accesstoken, debugging) {
/*
journeyid : id of the journey
version: version of the journey
access token: token was called from above
debugging: debug mode
*/
var endpoint = 'https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/interaction/v1/interactions/stop/' + journeyid + '?versionNumber=' + version;
var req = new Script.Util.HttpRequest(endpoint);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = 'application/json';
req.method = 'POST';
var auth = 'Bearer ' + accesstoken;
req.setHeader('Authorization', auth);
var resp = req.send();
var resultJSON = Platform.Function.ParseJSON(String(resp.content));
if (debugging) {
Write(Stringify(resultJSON) + '<br>');
};
}
function get_token(DEname, time, debugging) {
/*
DEname: DE stores access token
time: time at calling function
debugging: debugging mode
*/
// Client information
var client_id = 'YourClientID';
var client_sc = 'YourClientSecret';
var payload = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_sc
};
var url = 'https://YOUR_SUBDOMAIN.auth.marketingcloudapis.com/v2/token';
var req = new Script.Util.HttpRequest(url);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json";
req.method = "POST";
// auth = 'Bearer ' + accesstoken;
// req.setHeader("Authorization", auth);
req.postData = Stringify(payload);
var resp = req.send();
var resultJSON = Platform.Function.ParseJSON(String(resp.content));
if (debugging) {
Write('access token: ' + resultJSON.access_token + '<br>');
Write('expire in: ' + resultJSON.expires_in + '<br>');
};
var token_type = resultJSON.token_type
var access_token = resultJSON.access_token
var expires_in = resultJSON.expires_in //seconds
expires_in = expires_in * 1000 // convert to millisecond
var time_out = time + expires_in;
// store access token and other information into 'access token' DE
Platform.Function.UpsertDE(DEname, ['remark'], ['my token'],
['access_token', 'token_type', 'expires_in', 'time_out'],
[access_token, token_type, expires_in, time_out]);
return access_token;
}
</script>
Phù, vậy là xong, giờ thì test thử trên cloud page nào:
Nó sẽ trông tựa tựa thế này

Kiểm tra trên UI:

Vậy là ta đã hoàn thành xong việc dọn dẹp những journey cũ, rác, không dùng tới nữa rồi đấy. Chúc các bạn một ngày vui vẻ!