Julian
Julian

Reputation: 657

Typescript Parsing error: '}' expected when trying to use Google Analytics

I am trying to add Google Analytics to my react app and I have the Script tag in the head of the HTML file but I want this part of the script to be conditionally rendered. I don't want to use any dependencies if I can help it. The code below is what I am using but I get a Parsing Error. Can anyone shed any light on my dilemma?

import React from 'react';

interface IGoogleAnalytics {
  arguments?: any
}

const GoogleAnalytics: React.FC<IGoogleAnalytics> = () => {
  return (
    <script>
      {
        window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};
        ga.l=+new Date;
        ga('create', 'UA-000000000-1', 'auto');
        ga('send', 'pageview');
      }
    </script>
  )
}

export default GoogleAnalytics

Upvotes: 2

Views: 280

Answers (1)

Mohammad Rajabloo
Mohammad Rajabloo

Reputation: 2913

if you don't want use any dependence, so move your script tag to HTML file after Google Analytics script file.

<script src="link to Google Analytics script" />
<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};
    ga.l=+new Date;
    ga('create', 'UA-000000000-1', 'auto');
    ga('send', 'pageview');
</script>

Upvotes: 0

Related Questions