隱私權(&A);Messaging 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>
只在網頁瀏覽達到特定數量後顯示訊息
(假設您有自己的 Cookie 或其他機制來追蹤
次網頁瀏覽)
<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>
參閱新聞標記指南 (NTG),追蹤 Google Analytics 中允許廣告和廣告封鎖使用者的相關事件
請將 UA-xxxxxxxxx-x 替換成正確的帳戶追蹤 ID。
如需 News Tagging Guide 的詳細資訊,請參閱
請按這裡。
<!-- 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 替換成正確的帳戶追蹤 ID。
請參閱 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>
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eThe Funding Choices library enables publishers to control message delivery based on factors like traffic sampling, subscription status, page type, and pageview history.\u003c/p\u003e\n"],["\u003cp\u003eIt provides tools to integrate with Google Analytics, allowing publishers to track ad blocking behavior and user interactions with allow-ads prompts.\u003c/p\u003e\n"],["\u003cp\u003ePublishers can use these insights to understand ad blocking prevalence, user engagement, and tailor their messaging strategies accordingly.\u003c/p\u003e\n"],["\u003cp\u003eCode samples demonstrate how to implement these features using JavaScript and the Funding Choices API.\u003c/p\u003e\n"]]],[],null,["# Privacy & Messaging JavaScript API Samples\n\nShow the message to a random sample of your traffic\n---------------------------------------------------\n\n \u003cscript\u003e\n // Make sure that the googlefc property exists on the window.\n window.googlefc = window.googlefc || {};\n // To guarantee functionality, this must go before the tag on the page.\n googlefc.controlledMessagingFunction = (message) =\u003e {\n // Show the message to 10% of traffic.\n var percentageToShowTo = 10;\n\n // Pick a random number between 0 and 100.\n var rand = Math.random() * 100;\n\n if (rand \u003c= percentageToShowTo) {\n message.proceed(true);\n } else {\n message.proceed(false);\n }\n };\n \u003c/script\u003e\n\nDon't show the message to subscribers\n-------------------------------------\n\n(Assumes that you have a function with information on whether a user is a\nsubscriber) \n\n \u003cscript\u003e\n // Make sure that the googlefc property exists on the window.\n window.googlefc = window.googlefc || {};\n // To guarantee functionality, this must go before the tag on the page.\n googlefc.controlledMessagingFunction = (message) =\u003e {\n // checkSubscriptionStatus() is an example of a function that may exist\n // in your codebase that resolves a promise with true or false depending on\n // whether the user on the page is a subscriber.\n checkSubscriptionStatus().then(\n function (isSubscriber) {\n // Do not show the message if a user is a subscriber.\n if (isSubscriber) {\n message.proceed(false);\n } else {\n message.proceed(true);\n }\n }\n );\n }\n \u003c/script\u003e\n\nShow the message everywhere except your home page\n-------------------------------------------------\n\n \u003cscript\u003e\n // Make sure that the googlefc property exists on the window.\n window.googlefc = window.googlefc || {};\n // To guarantee functionality, this must go before the tag on the page.\n googlefc.controlledMessagingFunction = (message) =\u003e {\n var pathname = location.pathname;\n\n // This assumes other pages on your site are differentiated with a different\n // path. `location.href` can also be used if more information is needed to\n // differentiate between the home page and other pages on the site.\n if (pathname.length \u003e 1) {\n message.proceed(true);\n } else {\n message.proceed(false);\n }\n };\n \u003c/script\u003e\n\nOnly show the message after a certain number of pageviews\n---------------------------------------------------------\n\n(Assumes that you have your own cookie or other mechanism to track the number of\npageviews) \n\n \u003cscript\u003e\n // Make sure that the googlefc property exists on the window.\n window.googlefc = window.googlefc || {};\n // To guarantee functionality, this must go before the tag on the page.\n googlefc.controlledMessagingFunction = (message) =\u003e {\n // How many pageviews before the message is shown.\n var freePageviewsLimit = 3;\n\n // Check how many pages the user has seen.\n var pagesViewed = getPagesViewed();\n\n // Show the message if the user has seen more pages than the free limit.\n if (pagesViewed \u003e= freePageviewsLimit) {\n message.proceed(true);\n } else {\n message.proceed(false);\n }\n };\n \u003c/script\u003e\n\nTrack events associated with allow-ads and ad blocking users in Google Analytics with the News Tagging Guide (NTG)\n------------------------------------------------------------------------------------------------------------------\n\nReplace UA-xxxxxxxxx-x with the correct account tracking ID.\n\nMore information on the News Tagging Guide can be found\n[here](https://gweb-news-initiative-training.uc.r.appspot.com/training/datatools/ntg). \n\n \u003c!-- Google Analytics --\u003e\n \u003cscript\u003e\n window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;\n ga('create', 'UA-xxxxxxxxx-x', 'auto');\n ga('send', 'pageview');\n \u003c/script\u003e\n \u003cscript async src='https://www.google-analytics.com/analytics.js'\u003e\u003c/script\u003e\n \u003c!-- End Google Analytics --\u003e\n\n \u003cscript\u003e\n // Make sure that the googlefc property exists on the window.\n window.googlefc = window.googlefc || {};\n googlefc.callbackQueue = googlefc.callbackQueue || [];\n googlefc.callbackQueue.push({\n 'AD_BLOCK_DATA_READY': function() {\n switch (googlefc.getAdBlockerStatus()) {\n case googlefc.AdBlockerStatusEnum.EXTENSION_LEVEL_AD_BLOCKER:\n case googlefc.AdBlockerStatusEnum.NETWORK_LEVEL_AD_BLOCKER:\n ga('send', 'event', {\n eventCategory: 'NTG adblock',\n eventAction: 'detected',\n eventLabel: '\u003cpage url\u003e',\n nonInteraction: true\n });\n break;\n }\n switch (googlefc.getAllowAdsStatus()) {\n case googlefc.AllowAdsStatusEnum.ADS_ALLOWED:\n ga('send', 'event', {\n eventCategory: 'NTG adblock',\n eventAction: 'allow-ads',\n eventLabel: '\u003cpage url\u003e',\n nonInteraction: true\n });\n break;\n }\n }});\n \u003c/script\u003e\n\nTrack events in Google Analytics to determine users' ad blocking usage\n----------------------------------------------------------------------\n\nCan be used to determine what percentage of users is using a network-level ad\nblocker, extension-level ad blocker or no ad blocker.\n\nReplace UA-xxxxxxxxx-x with the correct account tracking ID.\n\nSee the [Google Analytics Docs](https://developers.google.com/analytics/devguides/collection/analyticsjs)\nfor more information about analytics. \n\n \u003c!-- Google Analytics --\u003e\n \u003cscript\u003e\n window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;\n ga('create', 'UA-xxxxxxxxx-x', 'auto');\n ga('send', 'pageview');\n \u003c/script\u003e\n \u003cscript async src='https://www.google-analytics.com/analytics.js'\u003e\u003c/script\u003e\n \u003c!-- End Google Analytics --\u003e\n\n \u003cscript\u003e\n // Make sure that the googlefc property exists on the window.\n window.googlefc = window.googlefc || {};\n googlefc.callbackQueue = googlefc.callbackQueue || [];\n googlefc.callbackQueue.push({\n 'AD_BLOCK_DATA_READY': function() {\n var analyticsData = {\n hitType: 'event',\n eventCategory: 'Funding Choices',\n eventAction: 'Ad Blocking Type'\n };\n switch (googlefc.getAdBlockerStatus()) {\n case googlefc.AdBlockerStatusEnum.EXTENSION_LEVEL_AD_BLOCKER:\n analyticsData.eventLabel = 'EXTENSION_LEVEL_AD_BLOCKER';\n ga('send', analyticsData);\n break;\n case googlefc.AdBlockerStatusEnum.NETWORK_LEVEL_AD_BLOCKER:\n analyticsData.eventLabel = 'NETWORK_LEVEL_AD_BLOCKER';\n ga('send', analyticsData);\n break;\n case googlefc.AdBlockerStatusEnum.NO_AD_BLOCKER:\n analyticsData.eventLabel = 'NO_AD_BLOCKER';\n ga('send', analyticsData);\n break;\n }\n }});\n \u003c/script\u003e"]]