text: import logger from "../../utils/logUtil";
  
  class GoogleTagManager {
    constructor(config) {
      this.containerID = config.containerID;
      this.name = "GOOGLETAGMANAGER";
    }
  
    init() {
      logger.debug("===in init GoogleTagManager===");
      (function (w, d, s, l, i) {
        w[l] = w[l] || [];
        w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
        const f = d.getElementsByTagName(s)[0];
        const j = d.createElement(s);
        const 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", this.containerID);
    }
  
    identify(rudderElement) {
      // set the traits to the datalayer and put everything under the key traits
      // keeping it under the traits key as destructing might conflict with `message.properties`
      const rudderMessage = rudderElement.message;
      const props = { traits: rudderMessage.context.traits };
      this.sendToGTMDatalayer(props);
    }
  
    track(rudderElement) {
      logger.debug("===in track GoogleTagManager===");
      const rudderMessage = rudderElement.message;
      const props = {
        event: rudderMessage.event,
        userId: rudderMessage.userId,
        anonymousId: rudderMessage.anonymousId,
        // set the traits as well if there is any
        // it'll be null/undefined before identify call is made
        traits: rudderMessage.context.traits,
        ...rudderMessage.properties,
      };
      this.sendToGTMDatalayer(props);
    }
  
    page(rudderElement) {
      logger.debug("===in page GoogleTagManager===");
      const rudderMessage = rudderElement.message;
      const pageName = rudderMessage.name;
      const pageCategory = rudderMessage.properties
        ? rudderMessage.properties.category
        : undefined;
  
      let eventName;
  
      if (pageName) {
        eventName = `Viewed ${pageName} page`;
      }
  
      if (pageCategory && pageName) {
        eventName = `Viewed ${pageCategory} ${pageName} page`;
      }
      if (!eventName) {
        eventName = "Viewed a Page";
      }
      const props = {
        event: eventName,
        userId: rudderMessage.userId,
        anonymousId: rudderMessage.anonymousId,
        // set the traits as well if there is any
        // it'll be null/undefined before identify call is made
        traits: rudderMessage.context.traits,
        ...rudderMessage.properties,
      };
  
      this.sendToGTMDatalayer(props);
    }
  
    isLoaded() {
      return !!(
        window.dataLayer && Array.prototype.push !== window.dataLayer.push
      );
    }
  
    sendToGTMDatalayer(props) {
      window.dataLayer.push(props);
    }
  
    isReady() {
      return !!(
        window.dataLayer && Array.prototype.push !== window.dataLayer.push
      );
    }
  }
  
  export { GoogleTagManager };
  