Skip to main content
Sitecore Documentation
  • Learn
  • Downloads
  • Changelog
  • Roadmap
CDPCloud Portal
Sitecore CDP
  • データブラウザ
  • 用語集
  1. JavaScript関数
  1. Sitecore CDP
  2. 視聴者輸出

コードスニペット

Markdown version
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

ここに、オーディエンスエクスポートで使える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}として出力に含めることができます。

また、アドバンストモードを使っている場合は、返されたJSONオブジェクトが自動的に出力に含まれます。

過去24時間の注文

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

(function () { load("classpath

.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}」として含めることができます。

また、アドバンストモードを使っている場合は、返された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

.078Z"}

Basicモードを使っている場合は、これを${jsResult.lastSessionDate}として出力に含めることができます。

また、アドバンストモードを使っている場合は、返されたJSONオブジェクトが自動的に出力に含まれます。

この記事を改善するための提案がある場合は、 お知らせください!

© Copyright , Sitecore A/S or a Sitecore affiliated company.
All rights reserved.

Privacy policySitecore Trust CenterTerms of use
CDPCloud Portal