View source: R/measurement_protocol.R
ga_mp_send | R Documentation |
Create a server side call to Google Analytics 4 via its Measurement Protocol
Use ga_mp_connection to set up the Measurement Protocol connections to pass to ga_mp_send. If using Google Tag Manager Server-Side, you can also set up a custom endpoint.
ga_mp_send(
events,
client_id,
connection,
user_id = NULL,
debug_call = FALSE,
timestamp_micros = NULL,
user_properties = NULL,
non_personalized_ads = TRUE
)
ga_mp_connection(
measurement_id,
api_secret = Sys.getenv("MP_SECRET"),
endpoint = NULL,
preview_header = NULL
)
events |
The events to send |
client_id |
The client_id to associate with the event |
connection |
The connection details created by ga_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 |
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 |
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.
TRUE
if successful, if debug_call=TRUE
then validation messages if not a valid hit.
Measurement Protocol (Google Analytics 4)
Other Measurement Protocol functions:
ga_mp_cid()
,
ga_mp_event_item()
,
ga_mp_event()
# preferably set this in .Renviron
Sys.setenv(MP_SECRET="MY_SECRET")
# your GA4 settings
my_measurement_id <- "G-1234"
my_connection <- ga_mp_connection(my_measurement_id)
a_client_id <- 123.456
event <- ga_mp_event("an_event")
## Not run:
#' ga_mp_send(event, a_client_id, my_connection, debug_call = TRUE)
# multiple events at same time in a batch
another <- ga_mp_event("another_event")
ga_mp_send(list(event, another),
a_client_id,
my_connection,
debug_call = TRUE)
# you can see sent events in the real-time reports
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)
## End(Not run)
# custom GTM server side endpoint
my_custom_connection <- ga_mp_connection(
my_measurement_id,
endpoint = "https://gtm.example.com",
preview_header = "ZW52LTV8OWdPOExNWFkYjA0Njk4NmQ="
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.