Utils
    • 4 Minutes to read
    • Dark
      Light

    Utils

    • Dark
      Light

    Article summary

    Browser

    With this method, you can check if the user is using a specific browser.

    isOpera

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isOpera();

    isIE

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isIE();

    isMSIE

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isMSIE();

    isTrident

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isTrident();

    isEdge

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isEdge();

    isFirefox

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isFirefox();

    isSafari

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isSafari();

    isChrome

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isChrome();

    isPhantom

    /**
     * @returns {boolean}
     */
    wtSmart.utils.browser.isPhantom();

    Cookie

    With the "cookie" method, you can set and get a cookie.

    /**
     * @param {string} name
     * @param {string} [value=""]
     * @param {number} [duration=0]
     * @param {string} [path="/"]
     * @param {string} [domain="current domain without subdomain"]
     * @param {boolean} [secure=false]
     *
     * @returns {string|boolean}
     */
    wtSmart.utils.cookie(name, value, duration, path, domain, secure);

    Example

    // set session cookie
    wtSmart.utils.cookie('session_cookie', '1');
     
    // set cookie with 6 month duration
    wtSmart.utils.cookie('ever_cookie', '1', 6*30*24*60);
    wtSmart.utils.cookie('ever_cookie', '1', 6*30*24*60, '/path', 'sub.domain.tld', true);
     
    // get cookie value
    var cookieValue = wtSmart.utils.cookie('cookie_name');

    Crypto

    URL

    With "URL" you have the option to "encode" and "decode" URI components.

    encode

    /**
     * @param {string} str
     *
     * @returns {string}
     */
    wtSmart.utils.crypto.URL.encode(str);

    decode

    /**
     * @param {string} str
     *
     * @returns {string}
     */
    wtSmart.utils.crypto.URL.decode(str);

    Example

    wtSmart.utils.crypto.URL.encode('äöü'); // %C3%A4%C3%B6%C3%BC
    
    wtSmart.utils.crypto.URL.decode('%C3%A4%C3%B6%C3%BC'); // äöü

    SHA256

    With "SHA256" you have the option to "encode" strings to a SHA256 hash.

    encode

    /**
     * @param {string} str
     *
     * @returns {string}
     */
    wtSmart.utils.crypto.SHA256.encode(str);

    Example

    wtSmart.utils.crypto.SHA256.encode('üöäÄÜÖ'); // c245f5277e70122d7e8427e6275cbd1a80f57cd7ec70a3e76f40d54a3687d02c

    MD5

    With "MD5" you have the option to "encode" strings to a MD5 hash.

    encode

    /**
     * @param {string} str
     *
     * @returns {string}
     */
    wtSmart.utils.crypto.MD5.encode(str);

    example

    wtSmart.utils.crypto.MD5.encode('üöäÄÜÖ'); // 17cbf9298930599d4c7f06b9d46cb187

    Event

    With "event" you have the option to "register" and "unregister" an event for an HTML Element.

    register

    /**
     * @param {HTMLElement|Window|Document} obj
     * @param {string} evt
     * @param {function(evt)} callback
     */
    wtSmart.utils.event.register(obj, evt, callback);

    unregister

    /**
     * @param {HTMLElement|Window|Document} str
     * @param {string} evt
     * @param {function(evt)} callback
     */
    wtSmart.utils.event.unregister(obj, evt, callback);

    Example

    var eventCallback = function(evt) {
        // do something after click
    };
    
    // register a click event
    for (var i = 0, l = document.links.length; i < l; i++) {
        wtSmart.utils.event.register(document.links[i], "click", eventCallback);
    }
    
    // unregister a click event
    wtSmart.utils.event.unregister(document.links[15], "click", eventCallback);

    Identifier

    everId

    /**
     * @param {string} [everId] - format (^\d{19}$)
     *
     * @returns {string}
     */
    wtSmart.utils.identifier.everId(everId);

    cdbeid

    /**
     * @returns {string}
     */
    wtSmart.utils.identifier.cdbeid();

    forceNewSession

    /**
     *
     */
    wtSmart.utils.identifier.forceNewSession();

    example

    // get existing Webtrekk Ever ID
    var everId = wtSmart.utils.identifier.everId();
     
    // set Webtrekk everId
    wtSmart.utils.identifier.everId('0123456789012345678');
    
    // get existing Webtrekk CDB ID
    var cdbeid = wtSmart.utils.identifier.cdbeid();

    Image

    With the "image" method, you can load an image asynchronously. Optionally, you can define a callback method and timeout that is called if the image is loaded or the timeout is reached.

    /**
     * @param {string} imageURL
     * @param {function(HTMLImageElement, string, number)} [callback]
     * @param {number=2000} [timeout]
     */
    wtSmart.utils.image(imageURL, callback, timeout);

    Example

    /**
     * @param {HTMLImageElement} img
     * @param {string} status - (success, error, timeout)
     * @param {number} executionTime
     */
    var imageCallback = function(img, status, executionTime)) {
        // do something
    };
    var imageURL = 'https://sub.domain.tld/track?page=start&t=' + (new Date().getTime());
     
    // load an image asynchronous
    wtSmart.utils.image(imageURL);
     
    // load an image asynchronous with callback
    wtSmart.utils.image(imageURL, imageCallback, 1000);

    Include

    With the "include" method, you can load an JavaScript file asynchronously. You can define optional a callback method which is called if the JavaScript file was loaded (configured property "required"). If the property "required" isn't configured, the callback method to be called after creating of DOM elements.

    /**
     * @param {string} scriptURL
     * @param {function()} [callback]
     */
    wtSmart.utils.include(scriptURL, callback);
    /**
     * @param {{
     *      src: string,
     *      [required=false]: boolean
     * }[]} scriptURL
     * @param {function()} [callback]
     */
    wtSmart.utils.include(scriptURL, callback);

    Example

    // load one js file
    wtSmart.utils.include('/path/to/my/js/file.js', function() {
        // do something and more
    });
     
    // load more js files
    wtSmart.utils.include(
        [
            {src: '/path/to/my/js/file_1.js', required: true},
            {src: 'sub.domain.tld/path/to/my/js/file_2.js'},
            {src: '//sub.domain.tld/path/to/my/js/file_3.js', required: true}
        ],
        function() {
            // do something and more, if 'file_1' and 'file_3' was successfully loaded
        }
    );

    OptOut

    With these methods you can get the current 1st party optout status or set a 1st and 3rd party optout cookie.

    getTrackingOptOut

    /**
     * @returns {boolean}
     */
    wtSmart.utils.optout.getTrackingOptOut();

    setTrackingOptOut

    /**
     * @param {number} [duration=5*12*30*24*60]
     * @param {function} [callback]
     */
    wtSmart.utils.optout.setTrackingOptOut(duration, callback);

    getAnonymousCookie

    Depending on the settings in the advanced configuration, the value "true" of the cookie refers to different tracking scenarios:

    • When setting the user-identifiable cookie by default and providing the possibility to opt-out of user-identifiable tracking, "true" refers to anonymous tracking and no user-identifiable cookie is set.

    • When using anonymous tracking by default, "true" means that the user agreed to user-identifiable tracking and the respective cookie and information is set and tracked.

    /**
     * @returns {boolean}
     */
    wtSmart.utils.optout.getAnonymousCookie();

    setAnonymousCookie

    /**
     * @param {number} [duration=5*12*30*24*60]
     */
    wtSmart.utils.optout.setAnonymousCookie(duration);

    deleteAnonymousCookie

    wtSmart.utils.optout.deleteAnonymousCookie();

    Example

    // get the optout status
    var optoutStatus = wtSmart.utils.optout.getTrackingOptOut();
    
    // set the optout cookie with a default duration of 5 years
    wtSmart.utils.optout.setTrackingOptOut();
    
    // set the optout cookie with a duration of 6 months
    wtSmart.utils.optout.setTrackingOptOut(6*30*24*60);
    
    // set the optout cookie with a duration of 6 months an execute afterwards the callback method
    wtSmart.utils.optout.setTrackingOptOut(6*30*24*60, function(img, status) {
        var message = 'The optout cookie was set successfully.';
        if (status !== 'success') {
            message = 'An error has occurred, please try again later.';
        }
    
        alert(message);
    });
    
    // get the anonymous cookie status
    var anonymous = wtSmart.utils.optout.getAnonymousCookie();
    
    // set the anonymous cookie with the default duration of 5 years
    wtSmart.utils.optout.setAnonymousCookie();
    
    // set the anonymous cookie with a duration of 6 months
    wtSmart.utils.optout.setAnonymousCookie(6*30*24*60);
    
    // delete the anonymous cookie
    wtSmart.utils.optout.deleteAnonymousCookie();

    Parameter

    With the "parameter" method, you can get a URL parameter value.

    /**
     * @param {string} param
     * @param {string} [url=document.location.href]
     * @param {*} [def=false]
     *
     * @returns {*}
     */
    wtSmart.utils.parameter(param, url, def);

    Example

    // get an url parameter value
    var parameterValue = wtSmart.utils.parameter('wt_mc', document.location.href, false);

    Referrer

    With the "referrer" method, you can get the referrer URL. Optionally, you can set a referrer URL which is used for the Smart Pixel as "origin" referrer URL.

    /**
     * @param {string} [referrer]
     *
     * @returns {string}
     */
    wtSmart.utils.referrer(referrer);

    Example

    // get referrer url
    var referrer = wtSmart.utils.referrer();
     
    // set referrer url
    wtSmart.utils.referrer('https://sub.domain.tld/path/to/page');

    Url

    With the "url" method, you can get the URL. Optionally, you can set a URL that is used for the Smart Pixel as "origin" URL.

    /**
     * @param {string} [url]
     *
     * @returns {string}
     */
    wtSmart.utils.url(url);

    Example

    // get page url
    var pageURL = wtSmart.utils.url();
     
    // set page url
    wtSmart.utils.url('https://sub.domain.tld/path/to/my/current/page');


    Was this article helpful?