Google Analytics in Perspective

We would like to embed Google Analytics code to Perspective web pages in order to see what pages our customers are using. Any guidelines where to put it?

1 Like

I spoke to @Carl.Gould at ICC and he told me this is possible, hopefully he can pitch in here and advise!

@Carl.Gould Could you comment this?

Are you able to figure out a way to add google analytics to your app?

Might be able to inject the google javascript with the markdown component.

1 Like

Using the markdown component:

	code = """<img style='display:none' 
         src='/favicon.ico' 
         onload="
            const scriptElement = document.querySelector('script#ga4tag');
            if (!scriptElement) {
                const script = document.createElement('script');
                script.async = true;
                script.id = 'ga4tag';
                script.src = 'https://www.googletagmanager.com/gtag/js?id=<your-ga-tag>';

                const configScript = document.createElement('script');
                configScript.innerHTML = \`
                    window.dataLayer = window.dataLayer || [];
                    function gtag(){dataLayer.push(arguments);}
                    gtag('js', new Date());
                    gtag('config', '<your-ga-tag>');
                \`;

                document.body.appendChild(script);
                document.body.appendChild(configScript);
            }
       ">
	</img>""".replace("\n", "").replace("\t", "").replace("<your-ga-tag>",value)

	return code
	

For something static like this that should work on every view, you could also use something like this to inject the script directly on the page without markdown

3 Likes

Spoilers: Analytics on the public demo project is exactly what we created the "injectables" stuff for.

3 Likes