Skip to content

Commit

Permalink
🐛 Partytown Reverse Proxy Fix (#1006)
Browse files Browse the repository at this point in the history
* Added debug partytown

* Added forward

* Added partytown scripts to build

* Added datalayer.push

* Fixed partytown implementation

* Added proxy

* Fixed to correct URL

* Updated partytown with correct configuration

* Removed unused packages, added schema validation

* Added hard coded URLs meant to be proxied

* Removed some hard coded links

* Added shared link whitelist, removed lint warning

* Fixed proxied implementation (scoping of variable in resolveUrl function must be in that function)

* Removed constants file, duplicated constants (required)

* Added builder.io proxy

* Removed proxy API route

* Added legacy google analytics to conditional

* Removed lib file, as copied over in build step anyway
  • Loading branch information
Harry-Ross authored Jul 14, 2023
1 parent ce89884 commit 04b540f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ yarn-error.log*
# Next bundle analyser
/analyze

# Partytown lib files
/public/~partytown

39 changes: 35 additions & 4 deletions components/layout/analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
import Script from "next/script";
import { Partytown } from "@builder.io/partytown/react";
import Head from "next/head";

const gtmId = process.env.NEXT_PUBLIC_GOOGLE_GTM_ID;

export const Analytics = () => {
return (
<Script id="google-tag-manager" strategy="worker">
{`
<Head>
<Partytown
debug={false}
forward={["gtag", "dataLayer.push", "_hsq.push, fbq"]}
resolveUrl={(url, location, type) => {
const proxiedWhitelist = [
"connect.facebook.net",
"snap.licdn.com",
"cdn3l.ink",
"googleads.g.doubleclick.net",
"js.hs-analytics.net",
"www.google.com",
];

if (
(type === "script" &&
!url.pathname.includes("gtm.js") &&
proxiedWhitelist.includes(url.hostname)) ||
url.href.includes("https://www.google-analytics.com/analytics.js")
) {
const proxiedUrl = new URL(
"https://cdn.builder.io/api/v1/proxy-api"
);
proxiedUrl.searchParams.append("url", url.href);
return proxiedUrl;
}
return url;
}}
/>
<script id="google-tag-manager" type="text/partytown">
{`
(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','${gtmId}');
`}
</Script>
</script>
</Head>
);
};
3 changes: 0 additions & 3 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const config = {
experimental: {
nextScriptWorkers: true,
},
i18n: {
locales: ["en"],
defaultLocale: "en",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "1.22.19",
"private": true,
"scripts": {
"dev": "tinacms dev -c \"node --require ./appInsight-api.js node_modules/next/dist/bin/next dev\"",
"build": "tinacms build && next build && next-sitemap --config next-sitemap.config.js",
"dev": "npm run partytown && tinacms dev -c \"node --require ./appInsight-api.js node_modules/next/dist/bin/next dev\"",
"build": "npm run partytown && tinacms build && next build && next-sitemap --config next-sitemap.config.js",
"partytown": "partytown copylib public/~partytown",
"postbuild": "next-sitemap --config next-sitemap.config.js",
"start": "tinacms build && node_modules/next/dist/bin/next start -p $PORT",
"export": "npm run build && next export",
Expand Down
2 changes: 1 addition & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ const App = ({ Component, pageProps }) => {

return (
<>
<Analytics />
<DefaultSeo {...NEXT_SEO_DEFAULT} />
<AzureAppInsights>
<Component {...pageProps} />
</AzureAppInsights>
<ZendeskButton zendeskKey={zendesk} />
<Analytics />
</>
);
};
Expand Down

0 comments on commit 04b540f

Please sign in to comment.