Skip to content

Commit 75edfb4

Browse files
Integrated GTM script on tutorials page.
1 parent feba49f commit 75edfb4

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

src/components/seo.jsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* See: https://www.gatsbyjs.org/docs/use-static-query/
66
*/
77

8-
import React from 'react';
8+
import React, { useEffect } from 'react';
99
import PropTypes from 'prop-types';
1010
import Helmet from 'react-helmet';
1111
import { useStaticQuery, graphql } from 'gatsby';
@@ -37,7 +37,34 @@ function SEO({ lang,
3737
`,
3838
);
3939

40-
const isIndexed = !noindex ? 'index, follow' : 'noindex, nofollow'
40+
const isIndexed = !noindex ? 'index, follow' : 'noindex, nofollow';
41+
42+
const loadGTM = window.location.href.includes("tutorials");
43+
44+
useEffect(() => {
45+
if (loadGTM) {
46+
const script = document.createElement('script');
47+
const noscript = document.createElement('noscript');
48+
script.type = 'text/javascript';
49+
script.innerHTML = `
50+
(function(w,d,s,l,i){
51+
w[l]=w[l]||[];
52+
w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});
53+
var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
54+
j.async=true;
55+
j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
56+
f.parentNode.insertBefore(j,f);
57+
})(window,document,'script','dataLayer','GTM-5F8HTVT');
58+
`;
59+
noscript.innerHTML = `
60+
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5F8HTVT"
61+
height="0" width="0" style="display:none;visibility:hidden">
62+
</iframe>
63+
`
64+
document.head.appendChild(script);
65+
document.head.appendChild(noscript);
66+
}
67+
}, []);
4168

4269
return (
4370
<Helmet

0 commit comments

Comments
 (0)