Jump to main content

Track users with Google Analytics on your Piranha site

2021-08-11 Piranha
Keep a track on your visitors and which pages are popular with Google Analytics. This post gives you a great example on how you can implement a dynamic way to add end edit your tracking codes

These code snippets will add a region on your site. To access it, simply click on the site while in the manager and you'll see a tab named "Analytics". Type in your Google analytics tracking code there and it should work.
This example also checks if your site is in production mode and that you are not logged in before adding the tracking scripts. This helps prevent Analytics from tracking you when you're managing or working on your own site.

MainSite.cs - Your default site model
[SiteType(Title = "Main site")]
public class MainSite : SiteContent<MainSite>
{
    [Region(Title = "Analytics")]
    [RegionDescription("Enter your Google Analytics code")]
    public StringField AnalyticsCode { get; set; }
}
_Layout.cshtml - Add the following in your Layout view
@* Place this code on the top of the file *@
@{
    MainSite mainSite = await WebApp.Api.Sites.GetContentByIdAsync<MainSite>(WebApp.Site.Id);
}


@* This piece can be placed at the bottom of your page before the closing body tag *@
@if (!String.IsNullOrEmpty(mainSite.AnalyticsCode) && !User.Identity.IsAuthenticated && !Env.IsDevelopment())
{
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=@mainSite.AnalyticsCode"></script>
    <script>
            window.dataLayer = window.dataLayer || [];
            function gtag() { dataLayer.push(arguments); }
            gtag('js', new Date());

            gtag('config', '@mainSite.AnalyticsCode');
    </script>

}
_ViewImports.cshtml - Separates usings and dependency injections. Create under /Views/Shared/_ViewImports.cshtml
@using Microsoft.Extensions.Hosting;

@inject Piranha.AspNetCore.Services.IApplicationService WebApp
@inject IHostEnvironment Env
How about another great post? Previous Next

You might also want to read the following

2021-08-12 Piranha

Edit block templates inside the manager with Scriban

Want to edit block templates without rebuilding your project? Let Scriban help you with that. Scriban parses your custom Html and objects and then gives you HTML ready to be used anywhere.

2021-08-10 Piranha

Manage your footer with a region

Footers are a basic component of almost all websites. Therefor we'll make it as easy as possible for us to manage ours by turning it into a region area on our site model. This guide uses Bootstrap 5

2021-08-08 Piranha

Carousel block for Piranha with Bootstrap

A good image slider or carousel can complete your website. This post will give you a quick example on how you can add your own carousel to your Piranha site

2021-08-07 Piranha

Youtube block for Piranha

Want to show Youtube videos on your Piranha site? This post will give you a quick example on how you could implement a Youtube block on your site, either for showing useful videos or Rick-Rolling your visitors

2021-08-05 Piranha

Accordion block with Bootstrap 5

Sometimes you have way to much text to fit on a page. A simple way to structure and group important content is by using an accordion. This post will give you an idea on how you can implement your own accordion on a Piranha site