Skip to content

Commit 6a65c50

Browse files
feat(analytics): add extra params prop to gtm
1 parent e5f85de commit 6a65c50

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

website/components/Analytics.tsx

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const getGTMIdFromSrc = (src: string | undefined) => {
1313
interface TagManagerProps {
1414
trackingId: string;
1515
src?: string;
16+
extraParams?: ExtraParams[];
1617
}
18+
1719
export function GoogleTagManager(props: TagManagerProps) {
1820
const _isOnPremises = !!props.src;
1921
const hasTrackingId = "trackingId" in props;
@@ -29,6 +31,14 @@ export function GoogleTagManager(props: TagManagerProps) {
2931
`/ns.html?id=${hasTrackingId ? props.trackingId : ""}`,
3032
hostname,
3133
);
34+
35+
for (const { key, value } of (props.extraParams ?? [])) {
36+
if (key && value) {
37+
src.searchParams.append(key, value);
38+
noscript.searchParams.append(key, value);
39+
}
40+
}
41+
3242
return (
3343
<>
3444
<Head>
@@ -103,6 +113,19 @@ const snippet = () => {
103113
});
104114
});
105115
};
116+
117+
/** @title {{key}} - {{value}} */
118+
interface ExtraParams {
119+
/**
120+
* @description key for extra param.
121+
*/
122+
key: string;
123+
/**
124+
* @description value for extra param.
125+
*/
126+
value: string;
127+
}
128+
106129
export interface Props {
107130
/**
108131
* @description google tag manager container id. For more info: https://developers.google.com/tag-platform/tag-manager/web#standard_web_page_installation .
@@ -118,13 +141,23 @@ export interface Props {
118141
* @description custom url for serving google tag manager.
119142
*/
120143
src?: string;
144+
/**
145+
* @description extra params for google tag manager url.
146+
*/
147+
extraParams?: ExtraParams[];
121148
/**
122149
* @description Disable forwarding events into dataLayer
123150
*/
124151
disableAutomaticEventPush?: boolean;
125152
}
126153
export default function Analytics(
127-
{ trackingIds, src, googleAnalyticsIds, disableAutomaticEventPush }: Props,
154+
{
155+
trackingIds,
156+
src,
157+
googleAnalyticsIds,
158+
disableAutomaticEventPush,
159+
extraParams,
160+
}: Props,
128161
) {
129162
const isDeploy = !!context.isDeploy;
130163
// Prevent breacking change. Drop this in next major to only have
@@ -137,14 +170,22 @@ export default function Analytics(
137170
{isDeploy && (
138171
<>
139172
{trackingIds?.map((trackingId) => (
140-
<GoogleTagManager src={src} trackingId={trackingId.trim()} />
173+
<GoogleTagManager
174+
src={src}
175+
trackingId={trackingId.trim()}
176+
extraParams={extraParams}
177+
/>
141178
))}
142179
{googleAnalyticsIds?.map((trackingId) => (
143180
<GTAG trackingId={trackingId.trim()} />
144181
))}
145182
{/* Drop this in next major to only have trackingId or trackingId and src */}
146183
{src && !trackingIds?.length && (
147-
<GoogleTagManager src={src} trackingId={trackingId} />
184+
<GoogleTagManager
185+
src={src}
186+
trackingId={trackingId}
187+
extraParams={extraParams}
188+
/>
148189
)}
149190
</>
150191
)}

0 commit comments

Comments
 (0)