Search code examples
google-analyticsmeasurement-protocol

Sending custom event data to Google Analytics


I need to track events in Google Analytics from a server through the Measurement Protocol. I can do this just fine, but my problem is that I want to send additional/custom data along with the event. Specifically, I want to send a UUID along with the event so that it is possible for me to fetch data from the Google Analytics API in the future and correlate events with rows in a relational database.

Is there any decent way to send custom data along with events? I looked at using the event value, but it must be an integer, and it is not intended for things like this. The event category, action, and label are reserved for other purposes.

I am not that proficient in Google Analytics, so the solutions off the top of my head would be:

  • Send an additional event containing the UUID in the event label or something like that. Seems like a bit of a hack/workaround to send two events, with one being used exclusively behind the scenes.
  • Perhaps using a custom dimension or metric. I am not 100% sure about the implications of this and if that's a decent approach or not.

So basically my question is: what would be the best way for me to send a UUID along with a Google Analytics event from a server, taking into consideration that I cannot use the event category, action, and label for the current event? Is there any other way in which I could link events retrieved from the Google Analytics API to rows in a database?


Solution

  • So let's say I trigger a "Completed Order" event to GA, and I also have orders in a MySQL database. So what I want to do, is to link an event to an order row in the database.

    There are several things that you can do and it pretty much depends on what you want to do with the information you are storing. For starters, all your requests should include the uid field with the value being the user ID within your system. This way all Google Analytics data will be calculated on the same user. Note: this is an internal value used within Google Analytics and you won't be able to see it.

    Second, I would create a custom dimension of the name user_id and store the user information in that. You will then be able to use this information within your reports to see what each user is doing. Note: it's against TOS to send user name, email, or any other PII (personally identifiable information) to Google Analytics. But you can send your internal user ID.

    I have done both of these in the past and found it to work quite well.

    More info on User-ID.