There are two ways to send a pageview to Google Analytics:
Default behavior
When you add gtag.js to your site, the snippet includes a config command that by default sends a pageview. You can include additional parameters to configure this behavior:
gtag('config', 'GA_MEASUREMENT_ID', <parameters>);
where <parameters>
is an object used to configure how Google Analytics is
initialized. When customizing the pageview behavior, the following keys may be
used:
Name | Type | Required | Default value | Description |
---|---|---|---|---|
page_title |
string |
No | document.title | The title of the page. |
page_location |
string |
No | location.href | The URL of the page. |
page_path |
string |
No | location.pathname |
The path to the page. If overridden, this value must start with a
|
send_page_view |
boolean |
No | true |
Whether or not a pageview should be sent. |
For example, the following overrides the page_title
and page_path
values:
gtag('config', 'GA_MEASUREMENT_ID', {
'page_title' : 'homepage',
'page_path': '/home'
});
Multiple properties
To send pageviews to multiple Google Analytics properties, specify each property
in a gtag('config')
call:
gtag('config', 'GA_MEASUREMENT_ID_1');
gtag('config', 'GA_MEASUREMENT_ID_2');
This is not a persistent setting and must be repeated on every page of your site
using the gtag.js
snippet.
Manual pageviews
For most Google Analytics implementations, the default snippet does not need to be modified. However, in cases where you want to manually control how pageviews are sent (e.g. single page applications or infinite scrolling), you should do the following:
Disable pageview measurement
To disable the default pageview hit, set the send_page_view
parameter to
false
in the gtag.js
snippet.
gtag('config', 'GA_MEASUREMENT_ID', {
send_page_view: false
});
The send_page_view
setting does not persist across pages. This setting must be
repeated on every page of your website where you want to disable automatic
pageviews.
If your snippet is configured for multiple properties, disable the pageview for each config command:
gtag('config', 'GA_MEASUREMENT_ID_1', {
send_page_view: false
});
gtag('config', 'GA_MEASUREMENT_ID_2', {
send_page_view: false
});
Manually send page_view
events
Where appropriate, make the following gtag
call, replacing placeholder values
as necessary:
gtag('event', 'page_view', {
page_title: '<Page Title>',
page_location: '<Page Location>',
page_path: '<Page Path>',
send_to: '<GA_MEASUREMENT_ID>'
})