cdp menu

Customerlabs CDP Documentation

Google Tag Manager (GTM) Integration

You are here:

Google Tag Manager is a tag management system by Google that allows you to tag and insert code on your website to collect website events and send them to analytics platforms like GA4.

By connecting Google Tag Manager with CustomerLabs, you can install the CustomerLabs tracking code easily and start tracking your website events by setting up the tags with the help of our snippets given in this document.

Install tracking code

Authenticate your Google Tag Manager account and install the tracking code by following the steps below:

1. Login to your CustomerLabs account

2. On the Home page → Under Track Website Events → Click Google Tag Manager. You will be redirected to authenticate your Tag Manager account.

3. After authentication, you will be asked to choose the tag manager account and container to install the CustomerLabs tracking code.

4. Once the GTag container is connected with CustomerLabs, the tracking code will be installed on the container.  

Note: Make sure you have already installed the connected GTag container on your website.

You can check the tag being created in your Tag Manager account with CustomerLabs tracking code.

Triggers & Tags

If you have already set up the triggers for any e-commerce events, you can use those triggers and create new tags with Custom HTML. You can then use the code snippets given below for tracking the default e-commerce events from the data layer.

If you haven’t set up any triggers, create new triggers based on the events that you want to track on your website from the data layer custom events, eg. view_item

Event to identify a user in the system

Create User event

Create User” event can be triggered to identify a user when the user submits a form, updates their contact information, etc, on your website.

You have to use “identify” call to select how you want to identify your users within CustomerLabs CDP. 

Whenever the identify method is called,  the “Create user” event is inside CustomerLabs for identification purposes inside the app. 

You can identify the users using email, phone, company, etc and also track additional user traits like first name, last name, address, etc.

For more details on how the identify call works, check out the detailed developer documentation

GTM code for triggering Create User

<script>
    var cl_create_user = setInterval(function(){
        if(((window.CLabsgbVar || {}).generalProps || {}).uid){
            var address = google_tag_manager["GTM-TAG-CODE"].dataLayer.get("orderData.customer.billing")
            if(address){
                var properties = {
                    "customProperties": {
                        "user_traits": {
                            "t": "Object",
                            "v": {
                                "first_name": {
                                    "t":"string",
                                    "v": address["first_name"]
                                },
                                "last_name": {
                                    "t":"string",
                                    "v": address["last_name"]
                                },                
                                "phone": {
                                    "t":"number",
                                    "v": address["phone"]
                                },
                                "email": {
                                    "t":"string",
                                    "v": address["email"]
                                },
                                "city": {
                                    "t":"string",
                                    "v": address["city"]
                                },
                                "postcode": {
                                    "t":"string",
                                    "v": address["postcode"]
                                },
                                "country": {
                                    "t":"string",
                                    "v": address["country"]
                                },
                            }
                        },
                        "identify_by_email": {
                            "t":"string",
                            "v": address["email"],
                            "ib": true
                        }
                    }
                }
                _cl.identify(properties)
            }
            clearInterval(cl_create_user)
        }
    }, 1000);

</script>

Default e-commerce events snippets

Product viewed

<script>
    function clProductsConversion(productsData){
        var products = [];
        for(var cli in  productsData){
            var product = productsData[cli];
            var newproduct = {};
            for(prodkey in product){
                switch(prodkey){
                case "id":
                    newproduct["product_id"] = product[prodkey];
                    break;
                case "name": 
                    newproduct["product_name"] = product[prodkey];
                    break;
                case "price":
                    newproduct["product_price"] = product[prodkey];
                    break;
                case "brand":
                    newproduct["product_brand"] = product[prodkey];
                    break;
                case "category":
                    newproduct["product_category"] = product[prodkey];
                    break;
                case "image":
                    newproduct["product_image"]= product[prodkey]
                default:
                    newproduct["product_" + prodkey] = product[prodkey];
                    break;
                }
            }
            products.push(newproduct);
        }
        return products;
    };
    function getProductValue(productsData){
        var val = 0;
        for(var cli in  productsData){
            var product = productsData[cli];
            var qty = 1;
            if(product["quantity"]){
                try{
                    qty = Number(product["quantity"]);
                }catch(e){}
            }
            var price = Number(product["price"])
            var product_price = qty * price
            val = val + product_price 
        }
        return val
    };

    var cl_product_viewed = setInterval(function(){
        if(((window.CLabsgbVar || {}).generalProps || {}).uid){
            var ecommerceEvent = window.google_tag_manager["GTM-TAG-CODE"].dataLayer.get("ecommerce");
            var properties = {}
            var value = getProductValue(ecommerceEvent.detail.products)
            properties["customProperties"] = {
                "currency": {
                    "t": "string",
                    "v": ecommerceEvent["currencyCode"]
                },
                "content_type": {
                    "t": "string",
                    "v": "product_group"
                },
                "value":  {
                    "t": "number",
                    "v": value
                }
            }
            properties["productProperties"] = clProductsConversion(ecommerceEvent.detail.products)
            _cl.pageview("Product Viewed", properties)
            clearInterval(cl_product_viewed)    
        }
    }, 1000);
</script>

Added to cart

<script>
    function clProductsConversion(productsData){
        var products = [];
        for(var cli in  productsData){
            var product = productsData[cli];
            var newproduct = {};
            for(prodkey in product){
                switch(prodkey){
                case "id":
                    newproduct["product_id"] = product[prodkey];
                    break;
                case "name": 
                    newproduct["product_name"] = product[prodkey];
                    break;
                case "price":
                    newproduct["product_price"] = product[prodkey];
                    break;
                case "brand":
                    newproduct["product_brand"] = product[prodkey];
                    break;
                case "category":
                    newproduct["product_category"] = product[prodkey];
                    break;
                case "image":
                    newproduct["product_image"]= product[prodkey]
                default:
                    newproduct["product_" + prodkey] = product[prodkey];
                    break;
                }
            }
            products.push(newproduct);
        }
        return products;
    };
    function getProductValue(productsData){
        var val = 0;
        for(var cli in  productsData){
            var product = productsData[cli];
            var qty = 1;
            if(product["quantity"]){
                try{
                    qty = Number(product["quantity"]);
                }catch(e){}
            }
            var price = Number(product["price"])
            var product_price = qty * price
            val = val + product_price 
        }
        return val
    };
    var cl_added_to_cart = setInterval(function(){
        if(((window.CLabsgbVar || {}).generalProps || {}).uid){
            var ecommerceEvent = window.google_tag_manager["GTM-TAG-CODE"].dataLayer.get("ecommerce");
            var properties = {}
            var value = getProductValue(ecommerceEvent.add.products)
            properties["customProperties"] = {
                "currency": {
                    "t": "string",
                    "v": ecommerceEvent["currencyCode"]
                },
                "content_type": {
                    "t": "string",
                    "v": "product_group"
                },
                "value":  {
                    "t": "number",
                    "v": value
                }
            }
            properties["productProperties"] = clProductsConversion(ecommerceEvent.add.products)
            _cl.trackClick("Added to cart", properties)
            clearInterval(cl_added_to_cart)    
        }
    }, 1000);
    
</script>

Checkout made

<script>
    function clProductsConversion(productsData){
        var products = [];
        for(var cli in  productsData){
            var product = productsData[cli];
            var newproduct = {};
            for(prodkey in product){
                switch(prodkey){
                case "id":
                    newproduct["product_id"] = product[prodkey];
                    break;
                case "name": 
                    newproduct["product_name"] = product[prodkey];
                    break;
                case "price":
                    newproduct["product_price"] = product[prodkey];
                    break;
                case "brand":
                    newproduct["product_brand"] = product[prodkey];
                    break;
                case "category":
                    newproduct["product_category"] = product[prodkey];
                    break;
                case "image":
                    newproduct["product_image"]= product[prodkey]
                default:
                    newproduct["product_" + prodkey] = product[prodkey];
                    break;
                }
            }
            products.push(newproduct);
        }
        return products;
    };

    function getProductValue(productsData){
        var val = 0;
        for(var cli in  productsData){
            var product = productsData[cli];
            var qty = 1;
            if(product["quantity"]){
                try{
                    qty = Number(product["quantity"]);
                }catch(e){}
            }
            var price = Number(product["price"])
            var product_price = qty * price
            val = val + product_price 
        }
        return val
    };


    var cl_checkout_made = setInterval(function(){
        if(((window.CLabsgbVar || {}).generalProps || {}).uid){   
            var ecommerceEvent = window.google_tag_manager["GTM-TAG-CODE"].dataLayer.get("ecommerce");
            var properties = {}
            var value = getProductValue(ecommerceEvent.checkout.products)
            properties["customProperties"] = {
                "currency": {
                    "t": "string",
                    "v": ecommerceEvent.checkout["currencyCode"]
                },
                "content_type": {
                    "t": "string",
                    "v": "product_group"
                },
                "value":  {
                    "t": "number",
                    "v": value
                }
            }
            properties["productProperties"] = clProductsConversion(ecommerceEvent.checkout.products)
            _cl.trackClick("Checkout Made", properties);
            clearInterval(cl_checkout_made)    
        }
    }, 1000);
    
</script>

Removed from cart

<script>
    function clProductsConversion(productsData){
        var products = [];
        for(var cli in  productsData){
            var product = productsData[cli];
            var newproduct = {};
            for(prodkey in product){
                switch(prodkey){
                case "id":
                    newproduct["product_id"] = product[prodkey];
                    break;
                case "name": 
                    newproduct["product_name"] = product[prodkey];
                    break;
                case "price":
                    newproduct["product_price"] = product[prodkey];
                    break;
                case "brand":
                    newproduct["product_brand"] = product[prodkey];
                    break;
                case "category":
                    newproduct["product_category"] = product[prodkey];
                    break;
                case "image":
                    newproduct["product_image"]= product[prodkey]
                default:
                    newproduct["product_" + prodkey] = product[prodkey];
                    break;
                }
            }
            products.push(newproduct);
        }
        return products;
    };
    function getProductValue(productsData){
        var val = 0;
        for(var cli in  productsData){
            var product = productsData[cli];
            var qty = 1;
            if(product["quantity"]){
                try{
                    qty = Number(product["quantity"]);
                }catch(e){}
            }
            var price = Number(product["price"])
            var product_price = qty * price
            val = val + product_price 
        }
        return val
    };

    var cl_removed_from_cart = setInterval(function(){
        if(((window.CLabsgbVar || {}).generalProps || {}).uid){   
            var ecommerceEvent = window.google_tag_manager["GTM-TAG-CODE"].dataLayer.get("ecommerce");
            var properties = {}
            var value = getProductValue(ecommerceEvent.remove.products)
            properties["customProperties"] = {
                "currency": {
                    "t": "string",
                    "v": ecommerceEvent["currencyCode"]
                },
                "content_type": {
                    "t": "string",
                    "v": "product_group"
                },
                "value":  {
                    "t": "number",
                    "v": value
                }
            }
            properties["productProperties"] = clProductsConversion(ecommerceEvent.remove.products)
            _cl.trackClick("Removed from cart", properties)
            clearInterval(cl_removed_from_cart)    
        }
    }, 1000);
</script>

Purchased

<script>
    var clProductsConversion = function(productsData){
        var products = [];
        for(var cli in  productsData){
            var product = productsData[cli];
            var newproduct = {};
            for(prodkey in product){
                switch(prodkey){
                case "id":
                    newproduct["product_id"] = product[prodkey];
                    break;
                case "name": 
                    newproduct["product_name"] = product[prodkey];
                    break;
                case "price":
                    newproduct["product_price"] = product[prodkey];
                    break;
                case "brand":
                    newproduct["product_brand"] = product[prodkey];
                    break;
                case "category":
                    newproduct["product_category"] = product[prodkey];
                    break;
                case "image_url":
                    newproduct["product_image"]= product[prodkey];
                    break;
                default:
                    newproduct["product_" + prodkey] = product[prodkey];
                    break;
                }
            }
            products.push(newproduct);
        }
        return products;
    };
    var _cl_purchase_event = setInterval(function(){
        if(((window.CLabsgbVar || {}).generalProps || {}).uid){
            var ecommerceEvent = window.google_tag_manager["GTM-TAG-CODE"].dataLayer.get("ecommerce");
            var properties = {};
            properties["customProperties"] = {
                "currency": {
                    "t": "string",
                    "v": ecommerceEvent["currencyCode"] || "USD"
                },
                "content_type": {
                    "t": "string",
                    "v": "product_group"
                },
                "value":  {
                    "t": "number",
                    "v": ecommerceEvent.purchase.actionField["revenue"]
                },
                "revenue":  {
                    "t": "number",
                    "v": ecommerceEvent.purchase.actionField["revenue"]
                },
                "shipping":  {
                    "t": "string",
                    "v": ecommerceEvent.purchase.actionField["shipping"]
                },
                "tax":  {
                    "t": "string",
                    "v": ecommerceEvent.purchase.actionField["tax"]
                },
                "transaction_id": {
                    "t": "string",      
                    "v": ecommerceEvent.purchase.actionField["id"]
                }
            }
            properties["productProperties"] = clProductsConversion(ecommerceEvent.purchase.products)
            _cl.pageview("Purchased", properties)
            clearInterval(_cl_purchase_event)
        }
    }, 1000);

</script>

B2B/Lead Gen events snippets

Complete Registration

var cl_complete_registration = setInterval(function(){
    if(window.CLabsgbVar && window.CLabsgbVar.generalProps){
        _cl.trackClick("CompleteRegistration");
        clearInterval(cl_complete_registration)    
    }
}, 500);

Contact form submitted

var cl_contact_form_submitted = setInterval(function(){
    if(window.CLabsgbVar && window.CLabsgbVar.generalProps){
        _cl.trackClick("ContactFormSubmitted");
        clearInterval(cl_contact_form_submitted)    
    }
}, 500);

Lead

var cl_lead = setInterval(function(){
    if(window.CLabsgbVar && window.CLabsgbVar.generalProps){
        _cl.trackClick("Lead")
        clearInterval(cl_lead)    
    }
}, 500);
Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
How Can We Improve This Article?
Table of Contents
CustomerLabs gives freedom, flexibility and wings to digital marketers.
Sign Up
© 2023, Customer Labs Inc. All rights reserved.

Schedule 1-1 meeting with first-party audience expert