MatteoGabriele/vue-gtag-next

View on GitHub
src/register-global-object.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { isBrowser } from "@/utils";
import { useState } from "@/state";

export default () => {
  if (!isBrowser()) {
    return;
  }

  const { globalObjectName, dataLayerName } = useState();

  if (window[globalObjectName.value] == null) {
    window[dataLayerName.value] = window[dataLayerName.value] || [];
    window[globalObjectName.value] = function () {
      window[dataLayerName.value].push(arguments);
    };
  }

  window[globalObjectName.value]("js", new Date());
};