0

I'm developing a js SDK that my clients will import in their Ecommerce. My SDK works similar to the Facebook social plugin for the like: it will render an iFrame showing an interactive button.

To get the analytics of this button, my SDK adds another dataLayer in the host page. Once the button is clicked, I trigger a ga custom event.

I'm inserting here the code. Considering that my client already has his own Google Analytics code, is the right way to do that? Is it even better if I rename my datalayer with a different name? For instance, right now my SDK does this:

var my_sdk = 
function()
{
  
/* ===== Load GTAG for WebWidgets ====== */
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.innerHTML = `
<!-- My Custom Google Tag Manager DataLayer  -->
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXXX');
<!-- End Google Tag Manager -->
`;
document.getElementsByTagName("head")[0].appendChild(script_tag);

and then


window.dataLayer = window.dataLayer || [];
window.dataLayer.push({'event'             : my_custom_event, 
                       'Page Hostname'    : window.location.hostname, 
                       'custom_event'     : 'true'
                      });

Should I replace dataLayer with myCustomLayer?

DeLac
  • 1,068
  • 13
  • 43

0 Answers0