בחירת קריאייטיב לפי תדירות

מריצים worklet של Shared Storage כדי לבחור כתובת URL ולעבד אותה במסגרת מגודרת.

Shared Storage API מאפשר פרטיות הצעת Sandbox למטרה כללית, אחסון באתרים שונים, שתומך בהרבה ותרחישים לדוגמה אפשריים. דוגמה אחת היא בקרת תדירות, שזמינה עבור לבדיקה ב-Chrome בטא 104.0.5086.0 ואילך.

מריצים סקריפט worklet כדי לבחור כתובת URL מרשימה נתונה, על סמך ואז לעבד את כתובת ה-URL הזו במסגרת מגודרת. אפשר להשתמש בהרשאה הזו כדי לבחור מודעות חדשות או תכנים אחרים כאשר מגיעים למגבלת התדירות.

בדיקת בחירת הקריאייטיב לפי תדירות

כדי לבדוק את בחירת הקריאייטיב לפי תדירות באמצעות Shared Storage ו-Fenced Frames, עליכם לאשר ב-Chrome מגרסה 104.0.5086.0 ואילך. הפעלת כל ממשקי ה-API לשמירה על פרטיות בפרסום במסגרת chrome://settings/adPrivacy.

אפשר להפעיל אחסון משותף גם באמצעות הדגל --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames בשורת הפקודה.

התנסות עם דוגמאות קוד

כדי לבחור כתובת URL אטומה וליצור אותה, צריך לרשום מודול worklet כדי לקרוא תוכן ששותפו של נתוני אחסון. מחלקה של worklet מקבלת רשימה של עד שמונה כתובות URL, ואז מחזירה את האינדקס של כתובת ה-URL שנבחרה.

כשהלקוח קורא ל-sharedStorage.selectURL(), ה-worklet מפעיל ומחזיר כתובת URL אטומה לצורך עיבוד למסגרת מוגדרת.

נניח שאתם רוצים לבחור מודעה אחרת או תוכן אחר להצגה בהתאם לתדירות של מספר הפעמים שהמשתמש ראה אותם בעבר. אפשר לספור כמה פעמים משתמש מסוים ראה תוכן, ולשמור את הערך באחסון המשותף. לאחר האחסון, הערך של נפח האחסון המשותף יהיה זמין עבורכם ממקורות שונים.

לאחר מכן, ה-worklet של האחסון המשותף קורא את הערכים שבאחסון המשותף, ומגדיל את המונה בכל תצוגה נוספת. אם הספירה לא הגיעה למגבלה המוגדרת מראש, מוחזר התוכן שאתם רוצים לעבד (אינדקס 1). אם לא, כתובת ה-URL של ברירת המחדל תוחזר (אינדקס 0).

בדוגמה הזו:

  • creative-selection-by-frequencyjs נטען דרך ה-iframe של מפיק התוכן או המפרסם, והוא אחראי לטעינת worklet של האחסון המשותף, ולרינדור האטום שהוחזר את המקור למסגרת מתוחכמת.
  • creative-selection-by-frequency-worklet.js הוא ה-worklet של האחסון המשותף שקורא את כדי לקבוע איזו כתובת URL מוחזרת בעקבות תוכן או קריאייטיב של מודעה.

creative-selection-by-frequency.js

// The first URL is the default content or ad to be rendered when the frequency limits reached.
const CONTENT_URLS = [
  { url: `https://${contentProducerUrl}/default-content.html` },
  { url: `https://${contentProducerUrl}/example-content.html` },
];

async function injectAd() {
  // Load the worklet module.
  await window.sharedStorage.worklet.addModule('creative-selection-by-frequency-worklet.js');

  // Set the initial frequency count
  window.sharedStorage.set('frequency-count', 0, {
    ignoreIfPresent: true,
  });

  // Run the URL selection operation to choose an ad based on the frequency count in shared storage.
  const fencedFrameConfig = await window.sharedStorage.selectURL('creative-selection-by-frequency', CONTENT_URLS, {
    resolveToConfig: true
  });

  // Render the opaque URL into a fenced frame
  document.getElementById('content-slot').config = fencedFrameConfig;
}

injectAd();

creative-selection-by-frequency-worklet.js

const FREQUENCY_LIMIT = 5;

class CreativeSelectionByFrequencyOperation {
  async run(urls, data) {
    // Read the current frequency limit in shared storage
    const count = parseInt(await sharedStorage.get('frequency-count'));

    // Check if the frequency limit has been reached.
    if (count === FREQUENCY_LIMIT) {
      console.log('Frequency limit has been reached, and the default content will be rendered.');
      return 0;
    }

    // Set the new frequency count in shared storage
    await sharedStorage.set('frequency-count', count + 1);
    return 1;
  }
}

// Register the operation as 'creative-selection-by-frequency'.
register('creative-selection-by-frequency', CreativeSelectionByFrequencyOperation);

Use cases

All available use cases for Select URL API can be found in this section. We'll continue to add examples as we receive feedback and discover new test cases.

Engage and share feedback

Note that the Select URL API proposal is under active discussion and development and subject to change.

We're eager to hear your thoughts on the Select URL API.

Stay Informed

  • Mailing List: Subscribe to our mailing list for the latest updates and announcements related to the Select URL and Shared Storage APIs.

Need Help?