Tracking virtual pageviews in Google Tag Manager (GTM) is a powerful method to measure non-pageview events that act like traditional pageviews. Virtual pageviews are useful for tracking activities or content viewed on your website that don’t involve loading a new page, such as interactions with dynamic content, single-page applications (SPAs), or tracking events like video plays, form submissions, or custom interactions.
Setting up and tracking virtual pageviews in GTM is straightforward. Below is a step-by-step guide:
What is a Virtual Pageview?
A virtual pageview simulates the behavior of a traditional pageview without actually loading a new page. When a user interacts with a specific event or action on your site (such as clicking a button or navigating a specific section of a single-page application), you can send this interaction as a “pageview” to Google Analytics, making it easier to measure user engagement on these types of dynamic actions.
Why Track Virtual Pageviews?
- Track Single-Page Applications (SPAs): For sites that don’t reload the page with each interaction (like SPAs), virtual pageviews let you track “pageviews” for different sections or events.
- Track Non-Pageview Actions: For non-pageview interactions (like button clicks, video views, or form submissions), sending virtual pageviews helps measure the content as if it were a new pageview.
- Track Specific User Engagement: It allows you to track specific parts of your site that don’t correspond to physical pageviews but are still important for user behavior analysis.
How to Set Up Virtual Pageviews in Google Tag Manager
Step 1: Create a Google Analytics Pageview Tag
The first step is to create a tag in GTM that will send the virtual pageview data to Google Analytics. Here’s how:
- Go to Google Tag Manager:
- Log in to your Google Tag Manager account.
- Create a New Tag:
- In the GTM dashboard, go to Tags and click New.
- Choose Tag Type:
- Select Google Analytics: Universal Analytics (if you are using Universal Analytics). For Google Analytics 4, you would use the Google Analytics: GA4 Event tag.
- Configure the Tag for Virtual Pageview:
- Tag Configuration: Choose the type of tag, depending on your setup:
- For Universal Analytics, choose Pageview as the Track Type.
- For GA4, you will configure an Event with a custom event name like “virtual_pageview”.
- Google Analytics Settings Variable: If you’ve already set up a Google Analytics Settings variable, select it here. Otherwise, you will need to manually enter your tracking ID (UA-XXXXX-X).
- Tag Configuration: Choose the type of tag, depending on your setup:
- Set the Virtual Page URL:
- This is the key to sending a virtual pageview. In GTM, you can dynamically set the page URL using GTM Variables. Typically, you will want to use a variable that represents the content or the action that you’re tracking. For example:
- You can create a variable that captures the page or action the user is interacting with (like a specific section ID or a button click).
- If you’re tracking a video view, you might use a variable that dynamically captures the title of the video or the event name.
Example for setting the virtual page URL:
- Page Path:
/video/intro-video
for tracking a video. - Page Title:
“Video Player: Intro Video Played”
for custom events.
- You can set the Virtual Page URL in the Page Path field, depending on your tracking requirements.
- If you want to track a custom event (e.g., a video play), you can pass the event name as a label or action.
- This is the key to sending a virtual pageview. In GTM, you can dynamically set the page URL using GTM Variables. Typically, you will want to use a variable that represents the content or the action that you’re tracking. For example:
- Set Other Fields (Optional):
- Page Title: You can also set the Page Title as a dynamic variable if required.
- Custom Dimensions or Metrics: If you have custom dimensions or metrics in your Google Analytics setup, you can pass them in the fields as well.
- Configure Trigger:
- For virtual pageviews, the trigger should be based on an action the user takes. Common triggers include:
- Click Trigger: For clicks on specific buttons or elements (e.g., a play button for a video).
- Pageview Trigger: For actions or transitions in a single-page application.
- Custom Event Trigger: If you’re tracking custom JavaScript events.
- Form Submission Trigger: To track form submissions.
For example, if you are tracking when a user clicks a button to play a video, your trigger could be a Click Trigger that looks for a button click.
- For virtual pageviews, the trigger should be based on an action the user takes. Common triggers include:
- Save the Tag:
- Once configured, give your tag a descriptive name like “Virtual Pageview – Video Played” and click Save.
Step 2: Create a Trigger for the Virtual Pageview
Now you need to create a trigger that will fire the virtual pageview tag when a specific action occurs.
- Go to Triggers:
- In GTM, go to the Triggers section and click New.
- Choose the Trigger Type:
- For most cases, you will want to use a Click Trigger or Custom Event Trigger, depending on how your website is structured.
Example triggers include:
- Click – All Elements: For tracking clicks on specific buttons or links.
- Custom Event: If you want to track a custom JavaScript event (such as a video play or user interaction).
- History Change Trigger: If you are using a single-page application, this trigger fires when the browser history changes.
- Configure the Trigger:
- For a Click Trigger, specify the conditions such as Click Element equals
#video-play-button
. - For a Custom Event Trigger, use the event name that corresponds to your action (e.g.,
video_played
).
- For a Click Trigger, specify the conditions such as Click Element equals
- Save the Trigger.
Step 3: Test Your Virtual Pageview Tag
Before you deploy the tag to your live site, you must test it thoroughly.
- Use GTM’s Preview Mode:
- Click on Preview in GTM to enable debugging mode.
- Open your website in the same browser and check the GTM preview console.
- Test the Trigger:
- Perform the action that should trigger the virtual pageview (e.g., clicking a play button for a video).
- Verify that the virtual pageview tag fires correctly in the GTM preview mode and check the Tags Fired tab.
- Check Google Analytics:
- Go to Real-Time in Google Analytics and verify that the virtual pageview is being recorded.
- Check that the Pageview data or custom events show up with the correct URL or action name.
Step 4: Publish the Container
Once you’ve verified that everything is working correctly:
- Go to GTM and click Submit to publish your container.
- Go Live with the changes to start tracking virtual pageviews on your live website.
Example Use Cases for Virtual Pageviews
- Video Plays:
- Track when users play a video on your site, even though it doesn’t trigger a page reload.
- Single-Page Applications:
- In SPAs, virtual pageviews simulate pageviews when users navigate between different views without reloading the page.
- Button Clicks and Interactions:
- Track important user interactions, such as clicks on call-to-action buttons, product views, or form submissions.
- Download Tracking:
- Track when users download files, such as PDFs, as virtual pageviews to measure engagement.
Conclusion
Setting up virtual pageviews in Google Tag Manager allows you to track non-pageview interactions as if they were pageviews, making it easier to analyze user behavior on dynamic or interactive websites. By using custom triggers and pageview tags, you can monitor user interactions, such as clicks, video plays, and form submissions, and send this data to Google Analytics. This helps ensure a better understanding of user engagement and allows for data-driven decision-making.