Migrating a C# Integration from GA3 to GA4

Updated on Saturday, May 6, 2023

GA4

This blog has a couple of Google Analytics integrations - the popular posts list is pulled from GA, and the unnecessarily accurate count of non visitors in the footer. I just migrated from the GA3 API to the GA4 API. The backend for this blog is ASP.NET MVC with .NET 4.8. One day I might catch up with the cool kids and try to get on .NET Core, but not today.

Here's where I stubbed my toe:

I'm following this code sample to make my first GA4 call. After installing the NuGet package I couldn't find BetaAnalyticsDataClient anywhere. It turns out that there is a Google.Apis.AnalyticsData.v1beta package and a Google.Analytics.Data.V1Beta which is only available if you check 'Include prerelease' when searching. You want the second one. I'm not in love with BetaAnalyticsDataClient as a class name, it suggests all sorts of breaking changes are coming. My GA3 integration has ticked over for years with no changes. Maybe GA4 is going to be more like Google Ads and shank you with breaking changes every few months. Moving on...

Wow the error messages are good. Kudos to the API team. I'm so used to cryptic bullshit but this API tells you what you're doing wrong and sends back helpful pointers and even URLs. Every API should be this friendly. I got through the remaining problems fairly quickly because of this.

The code sample passes the property ID as 'property/nnnnnnn' but the API is expecting 'properties/nnnnnnn'.

I'd been using a ServiceAccountCredential created from a .p12 file for GA3. This doesn't seem to be supported for BetaAnalyticsDataClient but I was able to generate a new credential with a .json serialization of the credentials and passing this to BetaAnalyticsDataClient worked fine. I had a permission denied error, this was because I hadn't added the service account email address to the property and doing so got me some data.

The client library is pretty classy (as in too many classes). Creating a filter to exclude internal users involves four nested classes - a FilterExpression that has another FilterExpression for a not condition and then this needs a Filter and the Filter needs a StringFilter. Tedious. And including enums for metrics and dimensions is too much trouble so adding those now requires Metric and Dimension classes but these are just initialized with a string. The list is here.

Lastly when it comes to running the thing the site won't start and says:

"CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'."

Presumably due to some NuGet horror or other. Adding that reference indeed fixes the problem and hopefully doesn't create a new one.

I am now technically if not emotionally prepared for GA3 to be switched off.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Migrating a C# Integration from GA3 to GA4 #code #ga4 #ithcwy #c# Some tips on avoiding pitfalls when migrating a C# application from Google Analytics 3 to Google Analytics 4. )

Send event parameters with every event and multiple tags in Google Analytics 4

gtag

There are some event parameters that are useful to send with every event. Google has a helpful guide here which even covers the case where you have multiple tags (I'm running GA4 and UA during the migration and this isn't unusual). You're supposed to call gtag set before calling config on each tag. This isn't working for me though, I see nothing coming through in the debug view.

Calling set before config works fine for user properties (my journey of discovery yesterday) but unless I was doing something stupid that I haven't seen yet no dice for event parameters. The code above uses the config method of initialization with a shared object to prevent duplicating code. This seems to work fine.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Send event parameters with every event and multiple tags in Google Analytics 4 #code #ga4 How to send common event parameters with every event in Google Analytics 4 )

User scoped custom dimensions in Google Analytics 4 using gtag

A user parameter in the GA4 debug view

Given that everything in Google Analytics 4 is an event I expected user scoped custom dimensions to be a regular event parameter as well. They're not. And most of the documentation both from Google and others talks about Google Tag Manager which doesn't help if you're just using gtag. It's not that hard to implement, but figuring out all the pieces was way harder than it should be. I hope this helps the next person...

To create a user scoped custom dimension you need a user property. This is different from an event property. In gtag you need to call set on user_properties with an object containing the user properties to set like this:

If you want the user property on the hit sent with page load do this right before the config call when loading your gtag (you can do it as part of the config call if you're just setting up one tag, or as above before the config call which is helpful if using more than one tag). If you don't know the value of the user property at page load time you can also set it and then send an event.

From the client you can inspect the beacon sent to GA4 for an event, for example:

Event parameters get a prefix of ep or epn. In this case I'm using a parameter rc_score set to 0.9 which appears in the beacon as epn.rc_score=0.9. GA auto-detects numeric values and uses epn for these. The user parameter gets a up prefix and in this case is up.user_quality=low. (In this specific case I'm sending a recapture score as both a custom metric and a user scoped dimension so I can segment out high and low quality users, at least from the perspective of recapture).

More visually you can use the debug view in the configure section of GA4 (why there are settings in the reporting interface as well as the settings interface I have no idea). To use this pass { 'debug_mode':true } to your gtag config call. To confirm that you're getting user properties look for an orange icon in the timeline (see screenshot at the top of this post). There is also a helpful user properties active now box at the bottom right of this screen.

Once you have this working you still need to wire it up in GA4. Wait 24 hours... then go to Configure -> Custom definitions. Add a new custom dimension, make sure you pick user scoped and you should then be able to select the user property to use to populate the dimension.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: User scoped custom dimensions in Google Analytics 4 using gtag #code #ga4 Step by step guide for sending user scoped custom dimensions to Google Analytics 4 via gtag. )

Email Alerts for new Referers in Google Analytics using Apps Script

Updated on Monday, February 13, 2023

Referral Traffic in Google Analytics

It's useful to know when you have a new website referrer. Google Analytics is plagued with spam referral and you want to filter this out of reporting as quickly as possible to stop it from skewing your data. It's also helpful to be able to respond quickly to new referral traffic - maybe leave a comment or promote the new link on social media.

The script below will send you a daily email with links to any new referrers (this is GA3, there is a GA4 version later in this post).

Start a new apps script project in Google Drive and paste in the code. At the top enter the view ID that you want to monitor and the email address that should receive reports.

Choose Advanced Google Services from the Resources menu and switch on the Google Analytics API. Then click the Google API Console link and enable the Google Analytics API there as well.

Finally pick Current project's triggers from the Edit menu and trigger the main function daily at a convenient time.

This script saves known referrers in script properties. For a site with lots of traffic this may run out of space in which case you might need to switch this out and write known referrers to a sheet instead.

For Google Analytics 4 properties use the version of the script below. The setup process is the same, but you need the Google Analytics Data API instead of the Google Analytics API.

Add your comment...

More Google Apps Script Projects

(All Code Posts)

(Published to the Fediverse as: Email Alerts for new Referers in Google Analytics using Apps Script #code #googleanalytics #appsscript #gas #ga4 Apps script that will email you any new referral traffic from Google Analytics. Useful for responding to new links and referrer spam. GA3 and GA4 versions. )