Google tag manager makes life so easier for use Google analytic. Because of GTM, marketers can handle the Google analytics without the help of web developer. In this post describe how to use Google tag manager with ASP.NET MVC to track page views of a web application. Setting up the GTM is simple and needs to create the container and put that JavaScript code in your base HTML page. When you use GTM, you have to deal with tags, rules and macros. Every tag must contain a rule to fire. Rule require a certain condition to trigger.
Let's go down with following steps to setup GTM with MVC applications.
1. Login into the Google tag manager and create the container. Enter account name and container name as below.
2. After create GTM container looks like as in below image. Container id shown with red color boxes.
3. Go to your “_Layout.cshtml” page of the MVC application. Copy and paste container content as shown in below, just right after opening tag of “<body>”. In this image shows red color box highlighting the GTM container id. This is a unique id and GTM and application use this id for communication.
4. Now you have to publish your application. I used my azure account to publish the application. After you successfully publish you will get your application URL. Using that URL sign up with Google analytic account. After successfully setup the analytic account, you can get your tracking id for the web application. Use the tracking ID to create GTM tags.
5. Create a tag as shown in below using analytic tracking ID. Select Tag type as “Universal Analytics” and track type as “Page View”
6. Add a rule for this tag as below. this rule will execute when relevant tag fire.
7. After all above steps publish your GTM. After you can see the results in your Google analytics dashboard.