Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Bronze Contributor
Bronze Contributor
 
6 REPLIES 6

Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Employee Emeritus

Hi Patrick, Yes! There is a way.

 

When you use the Web Companion you are actually just dropping a cookie to tell Tealium to switch environment: 

Tealium-Companion.png

 

 

Therefore, you can write a pre-loader extension to drop that cookie for you based on a query-string parameter. This unsupported but you can try using my extensions as a javascript extension scoped to 'pre-loader': 

 

/*
###############################################
### PROFILE/ENVIRONMENT SWITCHING EXTENSION ###
###############################################

Author : roshan@tealium.com

Notes:
This extension:

- Allows users to change profile or environment by adding 'tealium_env=[env name]':
    
    e.g. greatsite.co.uk?tealium_env=qa
    e.g. greatsite.co.uk?tealium_env=dev    

- Allows users to change profile by adding 'tealium_profile=[profile name]' 'tealium_env=[env name]':

    Note: If you don't change environment Tealium will force a change of environment to ensure the profile will change

    e.g. greatsite.co.uk?tealium_profile=greatprofile    
    e.g. greatsite.co.uk?tealium_profile=greatprofile&tealium_env=qa    

- Allows you to bust the cache by adding 'tealium_cb = 'true' 

    Note : This won't work without changing to a different environment to the original page

    e.g. greatsite.co.uk?tealium_env=dev&tealium_cb=true
    e.g. greatsite.co.uk?tealium_env=dev&tealium_profile=greatprofile&tealium_cb=true

    Note: You cannot cachebust on 'prod' environments

- To remove all environment switching, just add 'tealium_env=clear' to your query-string:

    e.g. greatsite.co.uk?tealium_env=clear


*/
// PROFILE/ENVIRONMENT SWITCHER

/*jslint browser: true, nomen: true, plusplus: true, regexp: true */
/*global utag: true, jQuery: true, b: true */

//Tealium Environment Switcher: MUST BE FIRST. DO NOT MOVE

//Update "profile" var to the name of the current tealium profile
(function () {

    "use strict";
    try {
        if (window.location.search && (window.location.search.indexOf("tealium_env=") > -1 || window.location.search.indexOf("tealium_profile") > -1) && !(window.utag_condload_env)) {

            /*        
            ### SET ALL OF THE RELEVANT VARS : For use in the rules ###
            Pull vars from the query string etc
            */

            var qs = window.location.search || "",
                env = qs.match(/(tealium_env=)(dev|qa|prod|clear)/),

                //Check to see if switching profile
                new_profile = qs.match(/(tealium_profile=)(.*)/),

                //Check for cachebuster
                cachebuster = qs.match(/(tealium_cb=)(true)/),

                a,
                b,
                //uncomment the correct line below to switch between different tealium domains
                tealium_domain = "//tags.tiqcdn.com", //multi-cdn
                //tealium_domain = "//tags-eu.tiqcdn.com", //multi-cdn (EU ONLY)
                //tealium_domain = "//tealium.hs.llnwd.net/o43", //limelight only
                src;

            //Update "account" & "profile" var to the name of the current tealium profile:                    
            var scripts = document.getElementsByTagName('script');
            var script_array = [];

            for (var i = 0; i < scripts.length; i++) {
                script_array.push(scripts[i].outerHTML);

            }

            var script_string = script_array.join(';');

            // Set 'profile'
            var tealium_string = script_string.split('utag.js')[0].split('/');
            var profile = tealium_string[tealium_string.length - 3];

            // Set 'account'
            var account = tealium_string[tealium_string.length - 4];

            //Clean new_profile
            if (new_profile) {
                new_profile = new_profile[2].split('&')[0];
            }

            //Set env
            if (env && env.length && env[2]) {
                env = env[2];
            }

            /*        
            ### CHECK CHANGES TO BE MADE ###
            We'll check whether to change environment, bust the cache, switch profile or clear all settings
            */

            //Check cachebuster
            if (cachebuster) {
                cachebuster = "?_cb=" + Math.random() * 10000000000000000000;
            } else {
                cachebuster = "";
            }

            //Check for environment switch
            if (env) {
                var tealium_env_switch = true;
            }

            //Check for profile switch
            if (new_profile) {
                if (new_profile.length && new_profile[2]) {
                    var tealium_profile_switch = true;

                    //If no environment change specified - add it
                    if (!env) {
                        env = tealium_string[tealium_string.length - 2];

                        //Change 'env' to a new env to ensure that it will change profile
                        if (env === 'prod' || env === 'qa') {
                            env = 'dev';
                        } else {
                            env = 'qa';
                        }

                        //Flag so Tealium changes environment
                        var tealium_env_switch = true;
                    }

                }

            }

            //Check for tealium reset (i.e. clear all settings and revert to standard settings)
            if (tealium_env_switch === true) {
                if (env.indexOf("clear") > -1) {
                    document.cookie = "utag_env_" + account + "_" + profile + "=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT";
                    if (window.console) {
                        window.console.log("Custom Tealium environment CLEARED. Default environment for this page will be used.");
                    }
                    //reload the page to switch back to default environment
                    window.location.search = "";
                    var stop = true;
                }
            }

            //Log what is happening to the console
            if ((tealium_profile_switch === true || tealium_env_switch === true) && stop !== true) {

                if (window.console) {
                    if (tealium_env_switch === true) {
                        window.console.log("tealium environment = '" + env + "'");
                    }
                    if (tealium_profile_switch === true) {
                        window.console.log("tealium profile = '" + new_profile + "'' and will change after page refresh");
                    }
                    if (cachebuster) {
                        window.console.log("tealium cachebuster is ON");
                    }
                }

                window.utag_condload_env = true;
                window.utag_condload = true;

                //Switch profile if set
                if (tealium_profile_switch !== true) {
                    new_profile = profile;
                }

                //Define new path for cookie
                src=tealium_domain + "/utag/" + account + "/" + new_profile + "/" + env + "/utag.js" + cachebuster;

                /*        
                ### RUN CHANGES : Now we'll start making changes ###
                Rewrite Tealium cookie/inject new utag.js into page based on above rules to change settings
                */

                // Check if cookie exists - if it does then we'll need to reload the page in a moment
                if (document.cookie.indexOf("utag_env_" + account + "_" + profile) > -1) {
                    var reload = true;
                }
                document.cookie = "utag_env_" + account + "_" + profile + "=" + src + ";path=/";

                //Refresh page if we need to (to ensure we load the desired profile)
                if (reload === true) {
                    window.location.search = "";
                } else {

                    // Otherwise inject the new utag.js into the page
                    a = document;
                    b = a.createElement('script');
                    b.language = 'javascript';
                    b.type = 'text/javascript';
                    b.src=src;
                    a.getElementsByTagName('head')[0].appendChild(b);
                }
            }
        }

    } catch (e) {
        console.log("Error: " + e);
    }
}());

 

Let me know if you have any Qs!

 

Thanks,

Roshan

Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Bronze Contributor
Bronze Contributor
Thanks, very useful.

Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Employee Emeritus
// For reference, here is the code to run in the Console to set the cookie. // (Replacing the upper-case values with actual lower-case values) document.cookie="utag_env_ACCOUNT_PROFILE=//tags.tiqcdn.com/utag/ACCOUNT/PROFILE/qa/utag.js; path=/; domain=YOURSITE.com";

Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Gold Contributor
Gold Contributor

Hi there,

Is this method still working? I could not replicate it.

Best regards,

Jose

Jose

Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Bronze Contributor
Bronze Contributor

Yes Its working. below is the console command.

document.cookie="utagdb=true"

Is there a way to switch Tealium environment without using the Tealium companion? Using a query string parameter for example? Otherwise is impossible to make tests with IE below version 10

Bronze Contributor
Bronze Contributor

There was a change to Web Companion back mid-July which removed the utag_env cookie capability. You can re-establish enabling the utag_env cookie capability by turning On the "Web Companion" Publish Configuration option then publishing to the prod environment:image.png

Public