user6698813 user6698813 - 1 month ago 10
ASP.NET (C#) Question

Google analytics Code doesn't work for webfom using master

where do i add my Google analytics tracking code in my asp website having Webform with master page.
when i add it to master page before

</head>
Tag , It only tracks the content of Master page.
But it want to track my Webform formed using Master page, what should i do??.

Answer

Add it on end of master page (before closing body tag) like this:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>

    <%--Google analytics and others javascripts api and plugin must be here--%>
    <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-1', 'auto');
        ga('send', 'pageview');    
    </script>
</body>
</html>

Any page that used this master page will tracked by analytics (GA). 48 hour after creating your property in GA you can track users real time in GA console.

Comments