Search code examples
angularjsgoogle-analyticsonsen-ui

I want to use google analytics in my hybird application which is using Onsen ui and angularjs


I want to use google analytics in my hybird application by using follows code in the index.html page:

<!DOCTYPE HTML>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
<script src="components/loader.js"></script>
<link rel="stylesheet" href="components/loader.css">
<link rel="stylesheet" href="css/style.css">
<script>
    // PhoneGap event handler
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        console.log("PhoneGap is ready");
    }
</script>
</head>
<body>
<script>
   !function(A,n,g,u,l,a,r){A.GoogleAnalyticsObject=l,A[l]=A[l]||function(){
   (A[l].q=A[l].q||[]).push(arguments)},A[l].l=+new Date,a=n.createElement(g),
   r=n.getElementsByTagName(g)[0],a.src=u,r.parentNode.insertBefore(a,r)
   }(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

   ga('create', 'UA-xxxxxx-x');
   ga('send', 'pageview');
</script>
<h1>HelloWorld!</h1>
</body>
</html>

But in the google analytics real-time overview page, I cannot see anything at there. Anybody knows how to archive it?


Solution

  • Try this that should work (it works for me)

    Put this before the </body> tag (notice that the tag is the closing one, so the <script> is inside the body.

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
    <script>
       !function(A,n,g,u,l,a,r){A.GoogleAnalyticsObject=l,A[l]=A[l]||function(){
       (A[l].q=A[l].q||[]).push(arguments)},A[l].l=+new Date,a=n.createElement(g),
       r=n.getElementsByTagName(g)[0],a.src=u,r.parentNode.insertBefore(a,r)
       }(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    
       ga('create', 'UA-XXXXX-X');
       ga('send', 'pageview');
    </script>