---
title: "コードスニペット"
slug: "code-snippets"
abstract: "Sitecore CDPのオーディエンスエクスポートで使うコードスニペット。"
---
# コードスニペット

ここに、[オーディエンスエクスポートで使](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/javascript-in-the-export.html#UUID-e0fcc0c5-b3e4-cbab-1a1c-81f5c3fee3e4_section-idm4541415161494433871232268395 "Computing data using functions")えるJavaScript関数の例を挙げます。

## ゲスト年齢

この関数は、ゲストの生年月日からのおおよその年齢を返します:

(function () {
 // Calculate age based on a date
 function getAge(date) {
 var today = new Date();
 var birthDate = new Date(date);
 var age = today.getFullYear() - birthDate.getFullYear();
 var m = today.getMonth() - birthDate.getMonth();
 if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
 age--;
 }
 return age;
 }

 // Calculate the guest's age based on their date of birth
 var guestAge = "";
 if (guest.dateOfBirth) {
 guestAge = getAge(guest.dateOfBirth);
 }

 // Return the guest's age in the "age" variable. Then, reference "age" as ${jsResult.age} on the Output structure tab.
 return { age: guestAge }
})()

リターン値の例: {"age": "25"}

Basicモードを使っている場合は、[これを${jsResult.age}として出力に含](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/data-in-the-export.html#UUID-b89e130c-ae01-8831-e80d-ef1c1ff4a1c8_section-idm4545492155870433871423726306 "Including data in the output")めることができます。

また、[アドバンストモード](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/javascript-in-the-export.html#UUID-e0fcc0c5-b3e4-cbab-1a1c-81f5c3fee3e4_section-idm234968722197438 "Basic and Advanced mode")を使っている場合は、返されたJSONオブジェクトが自動的に出力に含まれます。

## 過去24時間の注文

この関数は、ゲストが過去24時間以内に完了した注文を検索します。そのような注文が存在する場合、JavaScriptは注文参照と注文状況を返します:

(function () {
 load("classpath:moment.js");

 // Get guest's orders
 var orders = guest.orders;
 var orderRef = "";
 var orderStatus = "";

 // Get yesterday's date
 yesterdayDate = moment.utc().subtract(1, "day").format("YYYY-MM-DD");

 for (var orderCount in orders) {
 var orderDate = moment(ordersorderCount.orderedAt).format("YYYY-MM-DD");
 // Log last order date to the console
 console.log(orderDate);
 // Check if order date matches yesterday's date
 if (orderDate === yesterdayDate) {
 orderRef = ordersorderCount.referenceId;
 orderStatus = ordersorderCount.status;
 }
 }

 // Return the order data in the "orderRef" and "orderStatus" variables. Then, reference them as ${jsResult.orderRef} and ${jsResult.orderStatus} on the Output structure tab.
 return { orderRef: orderRef, orderStatus: orderStatus };
})();

リターン値の例: {"orderRef": "B-321741", "orderStatus": "PURCHASED"}

もし基本モードを使っているなら、これを出力に「${jsResult.orderRef}」と「${jsResult.orderStatus}[」として含め](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/data-in-the-export.html#UUID-b89e130c-ae01-8831-e80d-ef1c1ff4a1c8_section-idm4545492155870433871423726306 "Including data in the output")ることができます。

また、[アドバンストモード](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/javascript-in-the-export.html#UUID-e0fcc0c5-b3e4-cbab-1a1c-81f5c3fee3e4_section-idm234968722197438 "Basic and Advanced mode")を使っている場合は、返されたJSONオブジェクトが自動的に出力に含まれます。

## 直近の会期日

この関数はゲストの直近セッションの日付と時間を返します:

(function() {
 if (guest.sessions) {
 var lastSession = guest.sessions0;
 var date = "";

 if (lastSession) {
 if (lastSession.events) {
 for (var event in lastSession.events) {
 if (lastSession.eventsevent.createdAt) {
 date = lastSession.eventsevent.createdAt;
 }
 }
 }
 }
 }

 // Return the guest's last session date in the "lastSessionDate" variable. Then, reference "lastSessionDate" as ${jsResult.lastSessionDate} on the Output structure tab.
 return {
 "lastSessionDate": date
 };
})();

リターン値の例: {"lastSessionDate": "2026-04-15T09:11:07.078Z"}

Basicモードを使っている場合は、[これを${jsResult.lastSessionDate}として出力に含](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/data-in-the-export.html#UUID-b89e130c-ae01-8831-e80d-ef1c1ff4a1c8_section-idm4545492155870433871423726306 "Including data in the output")めることができます。

また、[アドバンストモード](https://doc.sitecore.com/cdp/ja/users/sitecore-cdp/audience-export/javascript-in-the-export.html#UUID-e0fcc0c5-b3e4-cbab-1a1c-81f5c3fee3e4_section-idm234968722197438 "Basic and Advanced mode")を使っている場合は、返されたJSONオブジェクトが自動的に出力に含まれます。