This page describes how to record user events in real time. User events are required for media recommendations. If you don't use media recommendations, importing user events isn't required. However, recording user events is recommended for media search apps.
For the user event types that you can record, see the eventType
field of the
userEvents
object. By ingesting user events, you can improve the
quality of your recommendations as well as the ranking of your search results.
Results with higher click-through rates are boosted, while results with lower
click-through rates are buried. Don't record user events for documents that you
have not yet imported.
If you are using a search app with multiple data stores attached, then Google
recommends that you ingest user events at the location level instead of the
data store level. This allows a given user event to be sent to the
data store that it's associated with. To ingest user events with documents from different
dataStores
at the location level rather than data store level,
see userEvents.write
.
To import historical user events in bulk, see Import historical user events.
You can record user events in the following ways:
For the JSON representation for user events, see
userEvents
in the API reference.
Before you begin
Before you import user events:
- Create a data store and an app.
- Review About user events for user event requirements.
Record real-time user events with a JavaScript pixel
To record user events with a JavaScript pixel:
- Create an API key for calls to the
userEvents.Collect
method - See JavaScript pixel example for an example of a JavaScript tag that collects user events
Create an API key
An API key is required if you use a JavaScript pixel to collect user events from users' browsers.
To create an API key for calls to the userEvents.Collect
method, complete the following steps:
In the Google Cloud console, go to the Credentials page.
In the project drop-down at the top of the Google Cloud console page, select your project (the project might already be selected).
Click Create credentials and then select API key. Don't add any referrer restrictions. Some user privacy settings are known to not pass the referrer URL.
- Take note of the generated API key, which you will use when calling user event logging.
For increased security, add an HTTP restriction to your API Key to restrict access to the Vertex AI Agent Builder service at
https://discoveryengine.googleapis.com/*
.
JavaScript pixel example
The following example records a view-item
user event using a JavaScript
pixel.
For the JSON representation for user events, see userEvents
in
the API reference.
<script type="text/javascript">
var user_event = {
"eventType" : "view-item",
"userPseudoId": "USER_PSEUDO_ID",
"userInfo": {
"userId": "USER_ID"
},
"attributionToken": "ATTRIBUTION_TOKEN",
"tagIds": "TAG_ID",
"documents": [
{
"id": "DOCUMENT_ID"
}
]
};
var _gre = _gre || {};
// Credentials for project.
_gre.apiKey = '[API_KEY]';
_gre.logEvent = user_event;
_gre.projectId = 'PROJECT_ID';
_gre.locationId = 'LOCATION_ID';
_gre.dataStoreId = 'DATA_STORE_ID';
(function() {
var gre = document.createElement('script'); gre.type = 'text/javascript'; gre.async = true;
gre.src = 'https://www.gstatic.com/discoveryengine/v1beta_event.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gre, s);
})();
</script>
Record real-time user events with the userEvents.write
method
REST
You can use the userEvents.write
method to send user events directly to the API from your backend server.
To record user events, send a POST request to the userEvents.write
method and
provide the appropriate request body. For the JSON representation for user
events, see userEvents
in the API reference.
If you have an app with multiple data stores, Google recommends that you
ingest events at the location level. In this case, use the endpoint
https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_NUMBER/locations/LOCATION/userEvents:write
.
export GOOGLE_APPLICATION_CREDENTIALS=/tmp/my-key.json
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'eventType': 'view-item',
'userPseudoId': 'visitor0',
'eventTime': '2020-01-01T03:33:33.000001Z',
'tagIds': ['321'],
'attributionToken': 'ABC',
'attributes': {
'example_text_attribute': {
'text': ['text_1', 'text_2']
},
'example_number_attribute': {
'numbers': [3.14, 42, 1.2345]
}
},
'documents': [{
'id': 'abc'
}],
'userInfo': {
'userId': 'abc',
'userAgent': 'Mozilla/5.0'
},
'pageInfo': {
'uri': 'http://example',
'referrerUri': 'http://example',
'pageViewId': 'currentPageUri'
}
}" \\
"https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_NUMBER/locations/global/dataStores/DATA_STORE_ID/userEvents:write"
C#
For more information, see the Vertex AI Agent Builder C# API reference documentation.
To authenticate to Vertex AI Agent Builder, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
For more information, see the Vertex AI Agent Builder Go API reference documentation.
To authenticate to Vertex AI Agent Builder, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
For more information, see the Vertex AI Agent Builder Java API reference documentation.
To authenticate to Vertex AI Agent Builder, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
For more information, see the Vertex AI Agent Builder Python API reference documentation.
To authenticate to Vertex AI Agent Builder, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
For more information, see the Vertex AI Agent Builder Ruby API reference documentation.
To authenticate to Vertex AI Agent Builder, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.