MatteoGabriele / vue-gtag

Global Site Tag plugin for Vue (gtag.js)
https://matteo-gabriele.gitbook.io/vue-gtag/
MIT License
857 stars 63 forks source link

[Feature request] Add support for Partytown #518

Open digitalcortex opened 1 year ago

digitalcortex commented 1 year ago

Page speed insights is complaining about Google Analytics module blocking the main thread. I figured that Partytown web worker can help avoid blocking the main thread when using analytics.

Is there any chance it could be supported out of the box?

angelov-a commented 1 year ago

+1

Adding support for Partytown is fairly straightforward.

It's a matter of setting a special type for the gtag script and notifying Partytown.

export const load = (url, options = {}) => {
  return new Promise((resolve, reject) => {
    if (typeof document === "undefined") {
      return;
    }

    const head = document.head || document.getElementsByTagName("head")[0];
    const script = document.createElement("script");

    /* ... */

    if (options.partytown) {
      script.type = 'text/partytown'; // mark the script for partytown
    }

    head.appendChild(script);

     if (options.partytown) {
      window.dispatchEvent(new CustomEvent('ptupdate')); // trigger partytown rescan
    }

    /* ... */
  });
};
kurwaBober commented 1 year ago

need this feature so much