The gtag.js snippet includes a config command that sends a pageview when a user visits a page on your site:
gtag('config', 'MEASUREMENT_ID');
You can add parameters after the measurement ID as follows to capture metadata about the pageview:
gtag('config', 'MEASUREMENT_ID', <parameters>);
For example, you can include the following optional parameters to capture the page URL and title:
gtag('config', 'MEASUREMENT_ID', {
page_title : 'My Profile',
page_location: 'https://example.com/me' // The full URL is required.
});
Note: When you configure a mobile app with Firebase, Analytics measures screenviews instead.
Disable pageviews
If you don’t want gtag.js to send a pageview, set the send_page_view
parameter to false
. The send_page_view parameter does not persist across pages. This setting must be repeated on every page of your website where you want to disable automatic pageviews.
gtag('config', 'MEASUREMENT_ID', {
send_page_view: false
});
Send pageviews
While Analytics sends a pageview by default, you can send your own page_view
event as follows:
gtag('event', 'page_view', {
page_title: 'My Profile',
page_location: 'https://example.com/me', // The full URL is required.
send_to: '<MEASUREMENT_ID>'
});
Analytics will use default values for any parameters that you don't set. Only set the send_to parameter when you want to send the page_view
event to a different property than the one configured in the default snippet.
Measure virtual pageviews
If your site loads page content dynamically and updates the document's URL, you might want to send additional pageviews to measure these virtual pageviews. To measure virtual pageviews, do one of the following:
- Rely on Enhanced measurement, which is enabled by default and sends a pageview when the browser history state changes.
- Manually measure virtual pageviews by relying on the default snippet behavior or the
page_view
event to send a pageview.
If you manually send page_view
events, make sure Enhanced measurement is configured correctly to avoid double counting pageviews on history state changes. Typically, this means disabling Page changes based on browser history events under the advanced settings of the Page views section.
- When a pageview is sent to Analytics, the default page parameter values are used, unless modified. This means you do not need to modify
page_title
orpage_location
parameters if updates towindow.location
(e.g. via the History API) anddocument.title
are made prior to the event being sent. - If your app relies on a library to handle page updates it's recommended that you review the behavior to confirm the library sets
window.location
anddocument.title
as expected. Alternatively, you can always explicitly set page parameters when sending apage_view
event to ensure accuracy and clarity.