[Experimental] Create a server side call to Google Analytics 4 via its Measurement Protocol

Use mp_connection to set up the Measurement Protocol connections to pass to mp_send. If using Google Tag Manager Server-Side, you can also set up a custom endpoint.

mp_send(
  events,
  client_id,
  connection,
  user_id = NULL,
  debug_call = FALSE,
  timestamp_micros = NULL,
  user_properties = NULL,
  non_personalized_ads = TRUE
)

mp_connection(
  measurement_id,
  api_secret = Sys.getenv("MP_SECRET"),
  endpoint = NULL,
  preview_header = NULL
)

Arguments

events

The events to send

client_id

The client_id to associate with the event

connection

The connection details created by mp_connection

user_id

Optional. Unique id for the user

debug_call

Send hits to the Google debug endpoint to validate hits.

timestamp_micros

Optional. A Unix timestamp (in microseconds) for the time to associate with the event.

user_properties

Optional. The user properties for the measurement sent in as a named list.

non_personalized_ads

Optional. Set to true to indicate these events should not be used for personalized ads.

measurement_id

The measurement ID associated with a stream

api_secret

The secret generated in the GA4 UI - by default will look for environment arg MP_SECRET

endpoint

If NULL will use Google default, otherwise set to the URL of your Measurement Protocol custom endpoint

preview_header

Only needed for custom endpoints. The X-Gtm-Server-Preview HTTP Header found in your GTM debugger

Value

TRUE if successful, if debug_call=TRUE then validation messages if not a valid hit.

Details

Create an API secret via Admin > Data Streams > choose your stream > Measurement Protocol > Create

To see event parameters, create custom fields in your GA4 account first, to see them in your reports 24hrs after you send them in with this function via Custom definitions > Create custom dimensions - dimension name will be how it looks like in the reports, event parameter will be the parameter you have sent in with the event.

user_id can be used for cross-platform analysis

timestamp_micros should only be set to record events that happened in the past. This value can be overridden via user_property or event timestamps. Events can be backdated up to 48 hours. Note microseconds, not milliseconds.

user_properties - describe segments of your user base, such as language preference or geographic location. See User properties

Ensure you also have user permission as specified in the feature policy

Invalid events are silently rejected with a 204 response, so use debug_call=TRUE to validate your events first.

See also

Measurement Protocol (Google Analytics 4)

Other Measurement Protocol functions: mp_cid(), mp_event_item(), mp_event()

Examples

# preferably set this in .Renviron Sys.setenv(MP_SECRET="MY_SECRET") # your GA4 settings my_measurement_id <- "G-1234" my_connection <- mp_connection(my_measurement_id) a_client_id <- 123.456 event <- mp_event("an_event") mp_send(event, a_client_id, my_connection, debug_call = TRUE)
#> 2021-04-10 11:21:38 > MP Request: https://www.google-analytics.com/debug/mp/collect?measurement_id=G-1234&api_secret=MY_SECRET #> { #> "client_id": "123.456", #> "non_personalized_ads": true, #> "events": [ #> { #> "name": "an_event" #> } #> ] #> }
#>
#> 2021-04-10 11:21:38 > Response: 200
#> 2021-04-10 11:21:38 > No validation messages found
#> [1] TRUE
# multiple events at same time in a batch another <- mp_event("another_event") mp_send(list(event, another), a_client_id, my_connection, debug_call = TRUE)
#> 2021-04-10 11:21:38 > MP Request: https://www.google-analytics.com/debug/mp/collect?measurement_id=G-1234&api_secret=MY_SECRET #> { #> "client_id": "123.456", #> "non_personalized_ads": true, #> "events": [ #> { #> "name": "an_event" #> }, #> { #> "name": "another_event" #> } #> ] #> }
#>
#> 2021-04-10 11:21:38 > Response: 200
#> 2021-04-10 11:21:38 > No validation messages found
#> [1] TRUE
if (FALSE) { # you can see sent events in the real-time reports library(googleAnalyticsR) my_property_id <- 206670707 ga_data(my_property_id, dimensions = "eventName", metrics = "eventCount", dim_filters = ga_data_filter( eventName == c("an_event","another_event")), realtime = TRUE) } # custom GTM server side endpoint my_custom_connection <- mp_connection( my_measurement_id, endpoint = "https://gtm.example.com", preview_header = "ZW52LTV8OWdPOExNWFkYjA0Njk4NmQ=" )