Skip to content

Commit

Permalink
Accept a nonce in our <GoogleAnalytics> fork
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinnl committed Feb 6, 2024
1 parent 9f28589 commit e7dae82
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/app/components/client/GoogleAnalyticsWorkaround.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"use client";

import { GAParams } from "@next/third-parties/dist/types/google";
import Script from "next/script";
import Script, { ScriptProps } from "next/script";
import { useEffect } from "react";

let currDataLayerName: string | undefined = undefined;

export const GoogleAnalyticsWorkaround = (props: GAParams) => {
const { gaId, dataLayerName = "dataLayer" } = props;
/**
* This component is based on <GoogleAnalytics> from `@next/third-parties`, but accepting a nonce
*
* @param props
*/
export const GoogleAnalyticsWorkaround = (
props: GAParams & { nonce: ScriptProps["nonce"] },
) => {
const { gaId, dataLayerName = "dataLayer", nonce } = props;

if (currDataLayerName === undefined) {
currDataLayerName = dataLayerName;
Expand Down Expand Up @@ -65,10 +72,12 @@ export const GoogleAnalyticsWorkaround = (props: GAParams) => {
gtag('config', '${gaId}');`,
}}
nonce={nonce}
/>
<Script
id="_next-ga"
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
nonce={nonce}
/>
</>
);
Expand Down

0 comments on commit e7dae82

Please sign in to comment.