Skip to content

feat(gatsby-plugin-google-gtag/tagmanager): Use Script Api for adding gtag scripts #36685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jawakarD
Copy link

@jawakarD jawakarD commented Sep 23, 2022

Description

  • We Introduced Script API but it's not being used in the official analytics plugins, so here I start with gtag plugin, I'm also planing to add the change to the tagmanager plugin in this PR too, as it's related.

Documentation

  • Update docs with new field strategy
  • Update docs to add partytownProxiedURLs for off-main-thread strategy
  • Update docs to add partytown-venilla-config to use proxy url for scripts tht doesn't send correct CORS headers like Universal google analytics.

Somthing like:

import React from "react";

export const onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    <script
      key="partytown-vanilla-config"
      dangerouslySetInnerHTML={{
        __html: `partytown = { 
          debug: true,
          resolveUrl(url, location) {
            if (url.href.includes('google-analytics.com/analytics.js')) {
              // Use a secure connection
              if (url?.protocol === 'http:') {
                url = new URL(url.href.replace('http', 'https'))
              }

              // Point to our proxied URL
              const proxyUrl = new URL(location.origin + '/__third-party-proxy')
              proxyUrl.searchParams.append('url', url)

              return proxyUrl
            }

            return url
          }
        }`,
      }}
    />,
  ]);
};

Related discussion

#36598

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 23, 2022
@jawakarD
Copy link
Author

I have a doubt if this window.gtag call will happen after the partytown initialization, so that we won't miss the first pageview event.

window.gtag(`event`, `page_view`, { page_path: pagePath })

@jawakarD jawakarD force-pushed the feat/use-script-api-for-google-gtag-and-tagmanager branch from 9c4f67f to 22afce3 Compare September 23, 2022 18:20
@jawakarD jawakarD changed the title feat: Use Script Api for adding gtag scripts feat(gatsby-plugin-google-gtag/tagmanager): Use Script Api for adding gtag scripts Sep 23, 2022
@kb1995
Copy link

kb1995 commented Sep 26, 2022

@jawakarD Would this PR also update the gatsby-plugin-google-tagmanager plugin or do I need to migrate to gatsby-plugin-google-gtag?

@jawakarD
Copy link
Author

@jawakarD Would this PR also update the gatsby-plugin-google-tagmanager plugin or do I need to migrate to gatsby-plugin-google-gtag?

I'm planning to update gatsby-plugin-google-tagmanager too after clearing the doubt I have. But working with tag-manger for off-main-thread is tricky, as we may or may not need to mention the scripts that tag manger will download to gatsby's partytownProxiedURLs. This might be a overhead.

https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-script/#resolving-urls

@burakozdemir32
Copy link

@jawakarD: any updates on this ?

@burakozdemir32
Copy link

I have a doubt if this window.gtag call will happen after the partytown initialization, so that we won't miss the first pageview event.

window.gtag(`event`, `page_view`, { page_path: pagePath })

This code snippet is from the documentation on how to integrate Partytown with gtag:

// gatsby-browser.js

export const onRouteUpdate = ({ location }) => {
  if (process.env.NODE_ENV !== 'production') {
    return null;
  }

  const pagePath = location ? location.pathname + location.search + location.hash : undefined;
  setTimeout(() => {
    if (typeof gtag === 'function') {
      gtag('event', 'page_view', { page_path: pagePath });
    }
  }, 100);
};

https://www.gatsbyjs.com/blog/how-to-add-google-analytics-gtag-to-gatsby-using-partytown/

@nrallakis
Copy link

@jawakarD Any updates?
Is there any way to load gtag without hurting pagespeed scores?

@Muata
Copy link

Muata commented Apr 25, 2023

Hey! any updates?

@jawakarD
Copy link
Author

jawakarD commented May 9, 2023

Is this still needed? I could look into it next week maybe.

@Muata
Copy link

Muata commented May 9, 2023

Is this still needed? I could look into it next week maybe.

Yes, please:)

@kb1995
Copy link

kb1995 commented May 10, 2023

Is this still needed? I could look into it next week maybe.

This would be amazing - looking for best ways to optimise my GTM script

@Muata
Copy link

Muata commented Jun 6, 2023

Hey, this is a significant issue when it comes to website optimization and obtaining better results through Google search. That's why I'm frequently asking: have you happened to take a look at it? 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants