Dynamically disable Advertising Features in Google Analytics

The preferred method of enabling and disabling Advertising Features, including Remarketing and Advertising Reporting Features, is to modify your property settings.

However, there may be times when you want to disable these features dynamically for a given page load or session, for example, when users have indicated they do not want their data used for the purpose of personalizing ads. You can accomplish this by modifying any version of the Analytics tracking code for which Advertising Features are available.

The following sections explain how to programmatically disable these features regardless of whether you've enabled them via your property settings or via your tracking code. The corresponding programmatic enablement instructions, also available elsewhere in our documentation, are provided here for reference.

Disable with Google Tag Manager

To disable advertising features when your Google Analytics tags are configured in Google Tag Manager, set the allowAdFeatures field to false in Fields to Set in your tag or settings variable:

  1. Open any relevant Universal Analytics tags or Google Analytics Settings variables. If your tag uses a Google Analytics Settings variable, you only need to make this change in the settings variable unless you want to override it on a tag by tag basis.
  2. Under More Settings > Fields to Set, select + Add Field.
  3. As the Field Name, enter allowAdFeatures.
  4. As the Value, enter false (or select a variable that returns false conditionally).

Disable with gtag.js

To disable display features using gtag.js, set the parameter allow_display_features to false in your property's config:

gtag('config', 'GA_TRACKING_ID', { 'allow_display_features': false });

Enable with gtag.js

It is not currently possible to enable Display Remarketing and the Advertising Reporting Features via code using gtag.js. If you use gtag.js and want to enable Display Remarketing and the Advertising Reporting Features, modify your property settings in Analytics.

Disable with analytics.js

To override the property settings to turn off Advertising Reporting Features:

Add ga('set', 'allowAdFeatures', false); after the create command, and before the hit is sent.

If you enabled Advertising Features via your tracking code:

Remove the line ga('require', 'displayfeatures');.

Enable with analytics.js

To enable these features for Universal Analytics, insert the bold line into your existing tracking code between the 'create' and 'send' commands, as in this example:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-XX', 'example.com');

ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>

Disable with ga.js

To override the property settings to turn off Advertising Reporting Features:

Add _gaq.push(['_set', 'displayFeatures', false]); after the create command, and before the hit is sent.

Enable with ga.js

If you're still using ga.js, you can enable Remarketing and Advertising Reporting Features for Standard Analytics by replacing the bold text in this example:

Standard tracking code

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-y']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;


ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

 

with the bold text in the example below:

Code modified to enable these features

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-y']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;


ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Was this article helpful?
How can we improve it?