Created
March 20, 2023 13:36
-
-
Save andresgcarmona/c3c919a2847bb93af9bed8f6f6b83299 to your computer and use it in GitHub Desktop.
Load Google Analytics Dynamically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const GoogleAnalytics = ({ ga_id }) => ( | |
<> | |
<script | |
async | |
src={`https://www.googletagmanager.com/gtag/js?id=${ga_id}`} | |
></script> | |
<script | |
dangerouslySetInnerHTML={{ | |
__html: ` | |
window.dataLayer = window.dataLayer || []; | |
function gtag(){dataLayer.push(arguments);} | |
gtag('js', new Date()); | |
gtag('config', '${ga_id}'); | |
`, | |
}} | |
></script> | |
</> | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
{process.env.GOOGLE_ANALYTICS_ID ? ( | |
<GoogleAnalytics ga_id={process.env.GOOGLE_ANALYTICS_ID} /> | |
) : null} | |
// ... other code... | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment