Example: Custom Purpose for Google Analytics

An example of delaying the loading of Google Analytics until the user to consents to a custom purpose.

Note: You may not need to gather consent for Google Analytics if you are not collecting User IDs or other pseudonymous identifiers.  We found this article helpful in explaining.

Configuring the custom purpose

This is an example of the configuration you'd provide to your account manager:

ID6ID is chosen to not collide with any other custom purpose used on your site
TitleGoogle AnalyticsThe title you wish to display to the user
Description

Allow Google Analytics to collect data and use cookies for the measurement of your use of content.
See Google Analytics terms for more details: https://www.google.com/analytics/terms/us.html

The description you wish to show 

Integrate on the page

The following code delays loading until the CMP has loaded and had prompted the user. See the google documentation for more details.

window.deployads = window.deployads || [];

// Check that the custom purpose has been accepted
window.deployads.push(function() {
  function doCustomWork() {
    // TODO: load google analytics tag here
  }
  if (deployads.__cmp) {
    deployads.__cmp('getPublisherConsents', null, function(result){

	  if (!result.gdprApplies || result.customPurposes[6]) {
        // If GDPR does not apply to this user or
        // GDPR does apply and the user has accepted the custom purpose
        doCustomWork();
      }
    });
  } else {
    // GDPR does not apply to this user
    doCustomWork();
  }
});