Skip to main content
Sitecore Documentation
  • Learn
  • Downloads
  • Changelog
  • Roadmap
CDPCloud Portal
Sitecore CDP
  • データ ブラウザー機能を使用する
    • オーディエンスの活性化のためのアイデア
    • オーディエンスのエクスポートを管理する
        • コード スニペット
        • ユーティリティ関数
    • オーディエンスのエクスポートのトラブルシューティング
  1. オーディエンスのエクスポート
  1. Sitecore CDP
  2. オーディエンスのエクスポート
  3. コード スニペット

コード スニペット

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

このページの翻訳は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 }
})()

戻り値の例: "25"

これを出力に次のように含めることができます ${jsResult.age}。

過去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(160, "day").format("YYYY-MM-DD");

  for (var orderCount in orders) {
    var orderDate = moment(orders[orderCount].orderedAt).format("YYYY-MM-DD");
    // Print last order date to the console
    print(orderDate);
    // Check if order date matches yesterday's date
    if (orderDate === yesterdayDate) {
      orderRef = orders[orderCount].referenceId;
      orderStatus = orders[orderCount].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 };
})();

戻り値の例: "B-321741", "PURCHASED"

これを ${jsResult.orderRef} および ${jsResult.orderStatus}として出力に含めることができます。

最新のセッションの日付

この関数は、ゲストの最新のセッションの日時を返します。

(function () {
  var lastSession = guest.sessions[0];
  var date = "";

  if (lastSession) {
    if (lastSession.events) {
      for (var event in lastSession.events) {
        if (lastSession.events[event].createdAt) {
          date = lastSession.events[event].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 };
})();

戻り値の例: "2025-04-15T09:11:07.078Z"

これを出力に次のように含めることができます ${jsResult.lastSessionDate}。

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

Documentation Assistant

This assistant uses AI to generate responses based on Sitecore documentation. While it has access to official sources, answers may be incomplete or inaccurate and should not be considered official advice or support.
Powered by
k
kapa.ai
Protected by reCAPTCHA

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

Privacy policySitecore Trust CenterTerms of use