Sourcepoint allows your organization to create a list of event callbacks that queued, ready to be executed once the script has loaded and Sourcepoint APIs are ready.

This article explains how this can be achieved with simple examples.


Functions to call

The functions your organization call should be methods of the window._sp_ object.

These include event handlers like 'addEventListener' or custom callback functions defined by your organization:

window._sp_queue = [];
window._sp_ = {
   config: {
      accountId: {ACCOUNT_ID},
      propertyHref: "{PROPERTY_URL}",
      baseEndpoint: "{CNAME_DOMAIN}",
      gdpr: {},
      events: {
         onMessageReady: function (message_type) {},
         onMessageChoiceSelect: function (message_type, choice_id, choice_type_id) {},
         onPrivacyManagerAction: function (message_type, pmData) {},
         onMessageChoiceError: function (message_type, err) {},
         onConsentReady: function (message_type, consentUUID, euconsent) {},
         onPMCancel: function (message_type) {},
         onMessageReceiveData: function (data) {},
         onSPPMObjectReady: function () {},
         onError: function (message_type, errorCode, errorObject, userReset) {}
      }
   },

   firstCustomFunction(data) {
      console.log('first custom function - ' + data);
   }

}

The custom functions are defined after the config section. The custom functions can be called anywhere in the CMP code using, for example:

window._sp_.firstCustomFunction(data);

Push event handlers, functions into a queue

Event handlers, custom functions can be queued using the window._sp_.queue() command.

The event handlers and custom functions will be ready to execute once the CMP script has loaded and the APIs are ready. This is immediately prior to the event callback onSPPMObjectReady being fired.

In the following example an event listener for 'onConsentReady' is added to the list and set to display the value of consentUUID. Also the handler loadPrivacyManagerModal will be called, opening the privacy manager for your campaign:

testdata = 22;
window._sp_queue.push(() => { 
   window._sp_.addEventListener('onConsentReady', (message_type, consentUUID, euconsent) => { console.log('onConsentReady called, consentUUID is - ' + consentUUID); }),
   window._sp_.gdpr.loadPrivacyManagerModal({PRIVACY_MANAGER_ID}), 
});

Please ensure that the array

window._sp_queue = [];

is defined beforehand in the CMP implementation code.


Simple example

A simple CMP configuration shown next demonstrates adding event listeners to a list using the window._sp_.queue() command.

This example also shows how a custom callback can be added to the list.

  Note: In this example, only the optional callback function onSPPMObjectReady is set for simplicity.

<script>
   window._sp_queue = [];
   window._sp_ = {
      config: {
         accountId: 1732,
         propertyHref: "https://www.visitdenmark.com",
         baseEndpoint: "https://cdn.privacy-mgmt.com",
         gdpr: { },
         events: {
           
            onMessageReady: function (message_type) {},    
            onMessageChoiceSelect: function (message_type, choice_id, choice_type_id) {},
            onPrivacyManagerAction: function (message_type, pmData) {},
            onMessageChoiceError: function (message_type, err) {},
            onConsentReady: function (message_type, consentUUID, euconsent) {},  
            onPMCancel: function (message_type) {},                
            onMessageReceiveData: function (data) {},
           
            onSPPMObjectReady: function () {
               console.log('onSPPMObjectReady called.');
            },

            onError: function (message_type, errorCode, errorObject, userReset) {}

         }
      },

      firstCustomFunction(data) {
         console.log('first custom function - ' + data);
      }

   }

   console.log("SP queue push call.");
   testdata = 22;
   window._sp_queue.push(() => { 
      window._sp_.addEventListener('onConsentReady', (message_type, consentUUID, euconsent) => { console.log('onConsentReady called, consentUUID is - ' + consentUUID); }),
      window._sp_.gdpr.loadPrivacyManagerModal(406661),
      window._sp_.firstCustomFunction(testdata) 
   });

</script>
<script src="https://cdn.privacy-mgmt.com/unified/wrapperMessagingWithoutDetection.js" async=""></script>

In this example, the privacy manager will open each time the webpage loads. The output in the browser console display should include the following lines:

SP queue push call.
Messaging without detection successfully executed.
first custom function - 22
onSPPMObjectReady called.
onConsentReady called, consentUUID is - 24f1c4f2-6375-4fc6-b0a5-ee10467da504_2
interaction complete XHR GET https://cdn.privacy-mgmt.com/mms/v2/message?message_id=406661 XHR GET https://cdn.privacy-mgmt.com/consent/tcfv2/privacy-manager/privacy-manager-view?siteId=16992&vendorListId=5ff5abeca228633aff0d2712&consentLanguage=EN XHR GET https://cdn.privacy-mgmt.com/consent/tcfv2/consent/v3/16992?consentUUID=247eca4e-fe32-474a-9507-ce0e280947e5_2&consentLanguage=EN&separateLegIntVendors=true
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.