# Top Page integration for Google Tag Manager (GTM)
This page will guide you through integrating Virtusize on the Top Page of your website using Google Tag Manager (GTM), which requires 5 steps:
1. Create a Custom HTML Tag
2. Adding the placeholders
We currently support one type of widgets:
- Fitting Room Discovery
This widget can be placed anywhere on your page, but it is recommended to specify the placement using placeholders. Adding placeholders will allow you to freely change the design of your page without worrying about breaking Virtusize widgets in the future.
3. Inserting Virtusize values
This step allows you to define custom variables where you can set the values required for Virtusize to work properly.
4. Integrating the Virtusize script
This script is necessary for calling our central application responsible for displaying our widgets.
5. Verification
# Before you start
Please contact your sales representative to receive a Store Name
.
# 1. Create a Custom HTML Tag
To create a custom HTML Tag select Add a new Tag from your workspace.
Click on Tag Configuration and select Custom HTML as the tag type.
# 2. Adding the placeholders
Please consult with your sales representative to determine the optimal positioning of the Virtusize tags.
If you are integrating the Virtusize Fitting Room Discovery (FRD) widget, please use the following JavaScript code by replacing frd-target-div
with the target element where you want to place the Fitting Room Discovery widget:
<script>
const targetDiv = document.querySelector(".frd-target-div");
if (targetDiv) {
targetDiv.append(Object.assign(document.createElement("div"), { className: "vs-placeholder-frd" }));
}
</script>
Once you've finished. Choose a trigger for when the tag should fire.
# 3. Inserting Virtusize values
There are default variables from GTM that can be used on Virtusize integration. If the variable needed is not available, you can create a new custom variable by clicking on the variables menu and selecting New
from the User-Defined Variables section.
- Choose Data Layer Variable as the variable type.
- Give the variable a name (e.g.,
vs-user-id
). - In the Data Layer Variable Name field, enter the name of the data layer key that holds the value you want to capture.
- Save the variable.
# 4. Integrating the Virtusize script
Inside the custom HTML you can now setup the variables. You can use the default ones provided by GTM or make a new variable to fetch other data required. In the example below, vs-user-id
variable was created and used inside the custom html.
<script type="text/javascript">
const vsObject = {
storeName: "STORE_NAME",
env: "ENVIRONMENT",
deviceType: "DEVICE_TYPE",
userId: "{{vs-user-id}}",
page: "topPage"
};
(function() {
window.vs = !window.hasOwnProperty("vs")
? vsObject
: Object.assign(window.vs, vsObject);
const { storeName, env, deviceType } = vsObject;
const script = document.createElement("script");
const firstScript = document.getElementsByTagName("script")[0];
const protocol = document.location.protocol ? 'https' : 'http'
Object.assign(script, {
async: true,
src: `${protocol}://integration.virtusize.com/${env}/${storeName}/${deviceType}.source.js`,
});
firstScript.parentNode.insertBefore(script, firstScript);
})();
</script>
value | explanation |
---|---|
STORE_NAME | This is the Store Name that you received from your sales representative. |
ENVIRONMENT | This can be either staging or production depending on what environment you are integrating on. |
DEVICE_TYPE | This can be either desktop or mobile depending on the device type the page is being accessed from. If your site is responsive (you serve the same resource to both desktop and mobile devices), please set this as desktop . |
vs-user-id | - Set empty string ('' ) when the user is not logged in. - Set the user's ID (using a hash function such as MD5, SHA-1, etc...) when the user is logged in. |
# Optimizing Virtusize Script Loading
The position of scripts in the page structure directly impacts loading speed. Placing the Virtusize script tag higher in the document's structure, preferably near the top, and within the header can significantly enhance the user experience.
Move Virtusize Script Near the Top Ensure the Virtusize script tag is included early in the page's lifecycle, ideally near the top of the document.
Place the Script in the Header Place the Virtusize script tag within the
<head>
section of your HTML document. By positioning Virtusize here, you ensure that it is loaded early in the page's lifecycle. Additionally, this placement strategy often places Virtusize above other third-party scripts, facilitating a smooth integration without potential conflicts.
Once you've finished. Choose a trigger for when the tag should fire.
# 5. Verification
After completing the integration, please share the Top Page URL with your sales representative.
To do so, find the version you would like to share by clicking on More Actions and selecting Share Preview.