You can call gtag()
commands anywhere on a page after the Google tag snippet.
The following commands are available with these gtag()
commands: config
and
set
establish general properties and event
sends data to Google products.
Initialize products with config
Use the config command to initialize and configure settings for a particular product account. The command has the following format:
gtag('config', '<TARGET_ID>', {<additional_config_info>});
<TARGET_ID>
is the ID of the product account to which you want to
send data, and the <additional_config_info>
is an optional object used to
specify additional configuration options.
The config
command enables you to specify which products and accounts gtag.js
should handle, and to specify associated configuration. Depending on the product
specified in TARGET_ID
, the config
command may also initiate certain
behavior for that product. For example, in some cases the config
command tells
gtag.js to initiate a pageview hit.
You should call the config
command only once per Google tag. Every subsequent
call should use the set
command.
To learn more about how the config
command
behaves in relation to individual products, read the product-specific
documentation:
- Google Ads conversions and remarketing
- Campaign Manager and Display & Video 360
- Search Ads 360
- Google Analytics
The most basic example consists of just the config
command and a TARGET_ID
:
gtag('config', '<TARGET_ID>');
You can adjust and extend a config
command by specifying parameters in the
optional <additional_config_info>
object. For example, adding the following
parameter will prevent a Google Analytics pageview from being automatically
sent:
gtag('config', 'TAG_ID', {'send_page_view': false});
Send data with event
The event command is how you send event data. For example, you can use
the event
command to send a login
event with a method
value of "Google":
gtag('event', 'login', {
'method': 'Google'
});
There is a set of recommended events, along with recommended parameters, that are useful in specific contexts. You can also send custom events that are not included in the list of recommended events.
Send data on every event with set
The set command allows you to set parameters
that will be associated with every subsequent event on the page. For example, if
all transactions on your site use the same currency, you can use the set
command to specify the currency
field:
gtag('set', {'currency': 'USD'});
You can set multiple attributes with a single set
command:
gtag('set', {
'country': 'US',
'currency': 'USD'
});