ตัวอย่าง JavaScript API เกี่ยวกับความเป็นส่วนตัวและการแสดงข้อความแจ้งผู้ใช้

แสดงข้อความแสดงตัวอย่างการเข้าชมแบบสุ่ม

<script>
  // Make sure that the googlefc property exists on the window.
  window.googlefc = window.googlefc || {};
  // To guarantee functionality, this must go before the tag on the page.
  googlefc.controlledMessagingFunction = (message) => {
    // Show the message to 10% of traffic.
    var percentageToShowTo = 10;

    // Pick a random number between 0 and 100.
    var rand = Math.random() * 100;

    if (rand <= percentageToShowTo) {
      message.proceed(true);
    } else {
      message.proceed(false);
    }
  };
</script>

ไม่ต้องแสดงข้อความต่อผู้ติดตาม

(สมมติว่าคุณมีฟังก์ชันที่มีข้อมูลว่าผู้ใช้เป็นสมาชิกหรือไม่)

<script>
  // Make sure that the googlefc property exists on the window.
  window.googlefc = window.googlefc || {};
  // To guarantee functionality, this must go before the tag on the page.
  googlefc.controlledMessagingFunction = (message) => {
    // checkSubscriptionStatus() is an example of a function that may exist
    // in your codebase that resolves a promise with true or false depending on
    // whether the user on the page is a subscriber.
    checkSubscriptionStatus().then(
      function (isSubscriber) {
        // Do not show the message if a user is a subscriber.
        if (isSubscriber) {
          message.proceed(false);
        } else {
          message.proceed(true);
        }
      }
    );
  }
</script>

แสดงข้อความทุกที่ยกเว้นหน้าแรกของคุณ

<script>
  // Make sure that the googlefc property exists on the window.
  window.googlefc = window.googlefc || {};
  // To guarantee functionality, this must go before the tag on the page.
  googlefc.controlledMessagingFunction = (message) => {
    var pathname = location.pathname;

    // This assumes other pages on your site are differentiated with a different
    // path. `location.href` can also be used if more information is needed to
    // differentiate between the home page and other pages on the site.
    if (pathname.length > 1) {
      message.proceed(true);
    } else {
      message.proceed(false);
    }
  };
</script>

แสดงข้อความหลังจากมีการดูหน้าเว็บถึงจํานวนที่กําหนดเท่านั้น

(สมมติว่าคุณมีคุกกี้ของตนเองหรือกลไกอื่นๆ เพื่อติดตามจํานวนการดูหน้าเว็บ)

<script>
  // Make sure that the googlefc property exists on the window.
  window.googlefc = window.googlefc || {};
  // To guarantee functionality, this must go before the tag on the page.
  googlefc.controlledMessagingFunction = (message) => {
    // How many pageviews before the message is shown.
    var freePageviewsLimit = 3;

    // Check how many pages the user has seen.
    var pagesViewed = getPagesViewed();

    // Show the message if the user has seen more pages than the free limit.
    if (pagesViewed >= freePageviewsLimit) {
      message.proceed(true);
    } else {
      message.proceed(false);
    }
  };
</script>

ติดตามเหตุการณ์ที่เกี่ยวข้องกับผู้ใช้ที่อนุญาตและการบล็อกโฆษณาใน Google Analytics ด้วยคู่มือการติดแท็กข่าว (NTG)

แทนที่ UA-xxxxxxxxx-x ด้วยรหัสการติดตามบัญชีที่ถูกต้อง

ดูข้อมูลเพิ่มเติมเกี่ยวกับคู่มือการติดแท็กข่าวได้ที่นี่

<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-xxxxxxxxx-x', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->

<script>
  // Make sure that the googlefc property exists on the window.
  window.googlefc = window.googlefc || {};
  googlefc.callbackQueue = googlefc.callbackQueue || [];
  googlefc.callbackQueue.push({
    'AD_BLOCK_DATA_READY': function() {
      switch (googlefc.getAdBlockerStatus()) {
        case googlefc.AdBlockerStatusEnum.EXTENSION_LEVEL_AD_BLOCKER:
        case googlefc.AdBlockerStatusEnum.NETWORK_LEVEL_AD_BLOCKER:
          ga('send', 'event', {
            eventCategory: 'NTG adblock',
            eventAction: 'detected',
            eventLabel: '<page url>',
            nonInteraction: true
          });
          break;
      }
      switch (googlefc.getAllowAdsStatus()) {
        case googlefc.AllowAdsStatusEnum.ADS_ALLOWED:
          ga('send', 'event', {
            eventCategory: 'NTG adblock',
            eventAction: 'allow-ads',
            eventLabel: '<page url>',
            nonInteraction: true
          });
          break;
      }
   }});
</script>

ติดตามเหตุการณ์ใน Google Analytics เพื่อระบุการใช้งานการบล็อกโฆษณาของผู้ใช้

ข้อมูลนี้ใช้ในการพิจารณาเปอร์เซ็นต์ของผู้ที่ใช้ตัวบล็อกโฆษณาระดับเครือข่าย ตัวบล็อกโฆษณาระดับส่วนขยาย หรือไม่มีตัวบล็อกโฆษณา

แทนที่ UA-xxxxxxxxx-x ด้วยรหัสการติดตามบัญชีที่ถูกต้อง

โปรดดูเอกสารของ Google Analytics สําหรับข้อมูลเพิ่มเติมเกี่ยวกับการวิเคราะห์

<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-xxxxxxxxx-x', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->

<script>
  // Make sure that the googlefc property exists on the window.
  window.googlefc = window.googlefc || {};
  googlefc.callbackQueue = googlefc.callbackQueue || [];
  googlefc.callbackQueue.push({
    'AD_BLOCK_DATA_READY': function() {
      var analyticsData = {
          hitType: 'event',
          eventCategory: 'Funding Choices',
          eventAction: 'Ad Blocking Type'
        };
      switch (googlefc.getAdBlockerStatus()) {
        case googlefc.AdBlockerStatusEnum.EXTENSION_LEVEL_AD_BLOCKER:
          analyticsData.eventLabel = 'EXTENSION_LEVEL_AD_BLOCKER';
          ga('send', analyticsData);
          break;
        case googlefc.AdBlockerStatusEnum.NETWORK_LEVEL_AD_BLOCKER:
          analyticsData.eventLabel = 'NETWORK_LEVEL_AD_BLOCKER';
          ga('send', analyticsData);
          break;
        case googlefc.AdBlockerStatusEnum.NO_AD_BLOCKER:
          analyticsData.eventLabel = 'NO_AD_BLOCKER';
          ga('send', analyticsData);
          break;
      }
   }});
</script>