Developer forum

Forum » Templates » Getting GoogleAnalyticsTrackingID on templates

Getting GoogleAnalyticsTrackingID on templates

Tomas Gomez
Reply

Hi,

I am working in an old Swift 1.6.1 to implement some GA4 events. A Swift update is desirable but not feasible right now.

I get the GoogleAnalyticsTrackingID on the Swift_master template by using Model.Area.Item.GetString("GoogleAnalyticsTrackingID") 

This code doesn't work on other templates because they inherit RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> instead of ViewModelTemplate<Dynamicweb.Frontend.PageViewModel>

How can I get the GoogleAnalyticsTrackingID  on those templates?

Regards,
Tomás


Replies

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Tomas,

You can try something like this:

@GetValue("Item.Area.GoogleAnalyticsTrackingID ")

It's an old, untested, approach

I hope it works.

Adrian

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

He cannot use GetValue as that is the old template tags.

Also he should not get that ID in other templates - something seems wrong.... Why is that needed when the script is included in the master?

Also the trackingid is set in a piece of js and the information is available as a js variable.

If he needs the raw value of the field, it can be access on the API - PageView.Current().Area.*

 
Tomas Gomez
Reply

Hi,

The purpose is to track the data of the purchases. These data are not managed by the script at Swift_Master so I added a new script at Receipt.chtm to send these data (see below) but not purchass are recorded at Google. I followed the example at https://doc.dynamicweb.com/forum/swift?ThreadID=84762

Is this the correct way to implement it? What am I doing wrong?

<script async src="https://www.googletagmanager.com/gtag/js?id=@googleAnalyticsMeasurementID"></script>
<script> 
    window.dataLayer = window.dataLayer || [];
    function gtag() {
        dataLayer.push(arguments);
        dataLayer.push({
            ecommerce : null // clear the previous ecommerce object
        });
        dataLayer.push({
            event: "purchase",
            ecommerce: { ...CONTENT_OF_THE_PURCHASES.... }
        }); 
    }
    gtag('js', new Date());
    gtag('config', '@googleAnalyticsMeasurementID');
</script>
 
 
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

No - this is wrong.

The script is already included in your header and should not be inserted again.

This would also overwrite and reset the window.dataLayer which is also already added in the header.

Swift comes with purchase reporting already - at least in later versions when you click the "Place order" button 

 
Tomas Gomez
Reply

Thank to clarify.

We already implemented the script in Swift_Master for a Swift 1.6.1. Google is receiving the data of the visits but not the data of the purchases.

What else do I need to do in order to receive the purchases?

The implemented at Swift_Master is this. The googleAnalyticsTrackingID has an empty value (because it is obsolete) and googleAnalyticsMeasurementID has a G-XXXXXXXXX value:

    @* Global site tag (gtag.js) - Google Analytics *@
    @if (!string.IsNullOrWhiteSpace(googleAnalyticsTrackingID) && allowTracking)
    {
        <script src="https://www.googletagmanager.com/gtag/js?id=@googleAnalyticsTrackingID" async></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag() { window.dataLayer.push(arguments); }
            gtag('js', new Date());
            gtag('config', '@googleAnalyticsTrackingID');
        </script>
    }

    @if (!string.IsNullOrWhiteSpace(googleAnalyticsMeasurementID) && allowTracking)
    {
        var GoogleAnalyticsDebugMode = "";
        bool isLoggedInBackendUser = false;

        if (Dynamicweb.Environment.ExecutingContext.IsAdminLoggedIn()) {
            isLoggedInBackendUser = true;
        }

        if (Model.Area.Item.GetBoolean("EnableGoogleAnalyticsDebugMode") && @isLoggedInBackendUser)
        {
            GoogleAnalyticsDebugMode = ", {'debug_mode': true}";
        }

        <script async src="https://www.googletagmanager.com/gtag/js?id=@googleAnalyticsMeasurementID"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', '@googleAnalyticsMeasurementID'@GoogleAnalyticsDebugMode);
        </script>
    }

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Latest Swift release is 1.19 - and this was not implemented until version 1.14 - see release notes.

 
Tomas Gomez
Reply

Yes, I know!

That is the reason I'm implementing it. As aforementioned, a Swift update is desirable but not feasible right now,  :-(

I implemented the script in Swift_Master and Google receives the visits but not the the purchases. I added an extra script on Receipt.cshtml, and now I know it was wrong thanks to your indications. 

What should I add to receive the purchase data on Google?

Regards,
Tomas

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

That would be described here:

https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag

 
Tomas Gomez
Reply

Thanks Nicolai,

One question (for DW 9.13.7 and Swfift 1.6.1): Are the purchase data received at Google when using the GTM script instead of the GA script? 

Regards,
Tomas

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Nope - since that was implemented in 1.14 and you have 1.6....

Votes for this answer: 1

 

You must be logged in to post in the forum