Skip to content

Commit

Permalink
feat: Add GTAG scripts (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Jul 6, 2023
1 parent e552b2b commit b8905b1
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions components/Analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { context } from "$live/live.ts";
import GoogleTagManager from "partytown/integrations/GTM.tsx";
import GoogleTagScript from "partytown/integrations/GTAG.tsx";
import Script from "partytown/Script.tsx";
import { sendAnalyticsEvent } from "../commerce/sdk/sendAnalyticsEvent.ts";

Expand All @@ -8,6 +9,12 @@ export interface Props {
* @description google tag manager container id. For more info: https://developers.google.com/tag-platform/tag-manager/web#standard_web_page_installation .
*/
trackingIds?: string[];
/**
* @title GA Measurement Ids
* @label measurement id
* @description the google analytics property measurement id. For more info: https://support.google.com/analytics/answer/9539598
*/
googleAnalyticsIds?: string[];
/**
* @description custom url for serving google tag manager. Set either this url or the tracking id
*/
Expand All @@ -19,21 +26,30 @@ export interface Props {
}

export default function Analtyics(
{ trackingIds, src, dangerouslyRunOnMainThread }: Props,
{ trackingIds, src, dangerouslyRunOnMainThread, googleAnalyticsIds }: Props,
) {
const isDeploy = !!context.isDeploy;
return (
<>
{/* TODO: Add debug from query string @author Igor Brasileiro */}
{/* Add Tag Manager script during production only. To test it locally remove the condition */}
{!!context.deploymentId && trackingIds && (
{isDeploy && trackingIds && (
trackingIds.map((trackingId) => (
<GoogleTagManager
trackingId={trackingId.trim()}
dangerouslyRunOnMainThread={dangerouslyRunOnMainThread}
/>
))
)}
{!!context.deploymentId && src && (
{isDeploy && googleAnalyticsIds && (
googleAnalyticsIds.map((trackingId) => (
<GoogleTagScript
trackingId={trackingId.trim()}
dangerouslyRunOnMainThread={dangerouslyRunOnMainThread}
/>
))
)}
{isDeploy && src && (
<GoogleTagManager
src={src}
dangerouslyRunOnMainThread={dangerouslyRunOnMainThread}
Expand Down

0 comments on commit b8905b1

Please sign in to comment.