The event command

The event command logs events as they occur on your site.

The syntax of the event command follows:

gtag('event', eventName, eventParameters);

where:

  • eventName is the name of the event being logged.
  • eventParameters (optional) is a JavaScript object containing a list of parameters associated with the event. The values of these parameters apply to this event only.

For example:

// Event with no parameters
gtag('event', 'sign_in')

// Event with parameters
gtag('event', 'sign_in', { 'method': 'google' })

Send events to specific Google Analytics properties

To send events to the default group, use the following event command:

gtag('event', 'event_name');

The default group is configured in the global tracking snippet:

gtag('config', 'GA_TRACKING_ID');

which is equivalent to the following:

gtag('config', 'GA_TRACKING_ID', { 'groups': 'default' });

To send events to a non-default group, use the config command to set the group and then specify the group as the value of the send_to parameter:

gtag('config', 'GA_TRACKING_ID', { 'groups': 'group_name' });
gtag('event', 'event_name', { 'send_to': 'group_name' });

To send events to Google Analytics properties that don't belong to any groups, specify the tracking IDs of the properties as the values of the send_to parameter:

gtag('event', 'event_name', { 'send_to': ['GA_TRACKING_ID_1', 'GA_TRACKING_ID_2'] });