Link to home
Create AccountLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net Google analytics tag not working

Hi

I am trying to get Google analytics to work with my ASP.net website. I was told to put the code below into the <head> section of every page, but as I use a Site.Master which has the <head> section, I only put it there. When I tested the connection on Google it didn't work. How do I solve this?
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-20xxx541-1">
</script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-20xx541-1');
</script>

Open in new window

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

When I tested the connection on Google it didn't work.  
Your tracking code as given seems correct. Just to make sure the censored GA ID is matching with your actual GA account ID.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-20xxx541-1">
</script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-20xx541-1');
</script>

Open in new window


Press Ctrl+U or Right click to choose View page source, check whether if you can see the tracking code present on your website, then it means Google Analytics is properly set up and is tracking your visitors. 

User generated image
When you first set up Google Analytics, it usually takes up to 24 to 48 hours to show your website traffic.
However, if you go to the real-time reports, you can see if Analytics is working properly or not.

User generated image

Avatar of Murray Brown

ASKER

Hi. Thanks. It definitely isn't working properly. I specifically want to know where to put the tag in the html given that I have a site master
You can put it before </body> tag to prevent slowing download js scripts from blocking page content. 
This is the standard practice as advised by Google when you install ga.js script in your web site.
User generated image
More information:
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingOverview

but it says the following
Option 2: Install a tracking tag in your website code
Paste your tracking tag before the closing </head> tag on every page of your website you want to track.

Do you might to share your existing master page that showing the tracking code? Have you check whether the rendered html code showing the GA tracking code that you added in master page?
Hi

Here it is

<%@ Master Language="VB" AutoEventWireup="true" CodeBehind="Site.master.vb" Inherits="Office2Web.SiteMaster" %>

<!DOCTYPE html>

<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="google-site-verification" content="AYotMr4IwpDfblx2i3nrEPJONtK2_pRPcaXqCbC77bM" />

    <title><%: Page.Title %></title>


    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>

    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,700&display=swap" rel="stylesheet">


    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-203xxxx41-1">
    </script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag() { dataLayer.push(arguments); }
        gtag('js', new Date());

        gtag('config', 'UA-203xxxx1-1');
    </script>



    
</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>


        <!-- Navbar -->
        <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" runat="server" href="~/">
                        <img src="Resources/images/logo/office2web-logo.svg" alt="The Office2Web logo"/>
                    </a>
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-collapse" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                </div>
                <div class="collapse navbar-collapse" id="main-menu-collapse">
                    <ul class="nav navbar-nav navbar-right">

                        <li><a style="color:Highlight; font-weight: bold;" href="javascript:void(Tawk_API.toggle())">Chat Live Now</a></li>
                        <li><a runat="server" href="~/">Home</a></li>
                        
                        <li><a runat="server" href="~/About">About</a></li>
                        <li class="dropdown">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">Services
                            <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a runat="server" href="~/microsoft-office-solutions">Microsoft Office Solutions</a></li>
                                <li><a runat="server" href="~/web-applications">Web Applications</a></li>
                                <li><a runat="server" href="~/accounting-services">Accounting Services</a></li>
                                <li><a runat="server" href="~/combined-web-and-office">Combined Web & Office</a></li>
                                <li><a runat="server" href="~/access-services">Access Services</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">Courses
                            <span class="caret"></span></a>
                            <ul class="dropdown-menu">

                                <li><a runat="server" href="~/excel-beginners">Excel Beginners</a></li>
                                <li><a runat="server" href="~/excel-intermediate">Excel Intermediate</a></li>
                                <li><a runat="server" href="~/excel-advanced">Excel Advanced</a></li>
                                <li><a runat="server" href="~/excel-vba-beginners">Excel VBA Beginners</a></li>
                                <li><a runat="server" href="~/excel-vba-intermediate">Excel VBA Intermediate</a></li>
                                
                            </ul>
                        </li>
                        <li><a runat="server" href="~/Contact">Contact</a></li>
                         <!--<li><a style="color:Highlight" href="javascript:void(Tawk_API.toggle())">Live Chat</a></li>-->
                        
                      
                    </ul>
                </div>
            </div>
        </nav>

        <script type="text/javascript">
            (function ($) {
                $(document).ready(function () {
                    $('ul.dropdown-menu [data-toggle=dropdown]').on('click', function (event) {
                        event.preventDefault();
                        event.stopPropagation();
                        $(this).parent().siblings().removeClass('open');
                        $(this).parent().toggleClass('open');
                    });
                });
            })(jQuery);
        </script>

        <!-- Content -->
        <div class="container-fluid p-0">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>
        </div>

        <!-- Footer -->
        <footer class="footer">
            <div class="container">
                <div class="footer-form">
                    <div class="row">
                        <div class="col-sm-12 col-md-6">
                            <h2>Chat online with us now</h2>
                     <a style="color:Highlight" href="javascript:void(Tawk_API.toggle())">Click here or on our chat icon</a>
                        </div>
                        <div class="col-sm-12 col-md-4">
                            <div class="form-group">
                                <!--<input type="email" id="inputEmail" runat="server" placeholder="Enter your email address" class="form-control" />-->
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-2">
                            <!--<button class="btn btn-primary" ID="btnGetStarted2" runat="server">Get started</button>-->
                             <asp:Button  CssClass="btn btn-primary" ID="btnGetStarted" runat="server" Text="Get started" Visible="false" />
             
                              <!--<a style="color:Highlight" href="javascript:void(Tawk_API.toggle())">Chat to us live now</a>-->
                            <br />
                               <!--<a href="javascript:void(Tawk_API.toggle())"> <img src="/Resources/CallCentre1.jpg"> </a>-->
                     
                            </div>
                        <asp:Label ID="lblEmailOnlyResult" runat="server" Text=""></asp:Label>
                    </div>
                </div>
                <hr />
                <div class="footer-bottom">
                    <div class="row">
                        <div class="col-sm-12 col-md-6">
                            <p>&copy; <%: DateTime.Now.Year %> - Office2Web Pty Ltd</p>
                        </div>
                    </div>
                </div>
            </div>
        </footer>
    </form>
</body>
</html>

Open in new window

my site is Office2Web.co.uk

there are few things to highlight here after i view your site at Office2Web.co.uk
1. Why don't you put the GA in GTM container since you have using GTM?
2. I saw two GA accounts assigned in your page. One is using Google Universal and another is using GA4. Which are you are going to use?

User generated image
3. Put your GA tracking code right before </body> tag and check again.
<!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-179472068-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag() { dataLayer.push(arguments); }
        gtag('js', new Date());

        gtag('config', 'UA-179472068-1');
    </script>

Open in new window


Check again once you have revise the tracking code arrangement as mentioned above. 
Hi
I am still not sure where you want me to put this. Is it on every page in the body just befor </body> tag ore on the site.master
if I put it on the Site.Master then surely it will cover all pages
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
the problem is that the <head> section is only on the site.master page. So you are saying that it needs to go in the <body> section