1

Hello I am new to angularJs and i want to implement google analytics script in my angular app, which is having more thann 20pages and i am using routing. If anyone can help me in that, I will be very gratefull. If you can give a example where to add the script and how. Thanks in advance.

2
  • Just paste the GA Tracking code in the common header template of your website. Commented Dec 22, 2016 at 9:03
  • Thanks for your response, but only pasting won't work in angularjs Commented Dec 24, 2016 at 19:24

1 Answer 1

1

Here I am providing answer to my question: First of all add you google analytics code to your index.html inside of script tag

<script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

          ga('create', 'UA-xxxxxxxx-x', 'auto');
          ga('send', 'pageview');
</script>

Then create a function in app.js file

$rootScope.$on('$locationChangeStart', function() {
            ga('set', 'page', $location.path());
            ga('send', 'pageview');
});

And you are good to go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.