Go to Qubit

Opentag documentation

Dependencies

Dependencies are used to ensure that tags fire in a particular order.

Overview

In the past you might have asked your developers to put one tag before the other in the page source – dependencies are just Opentag’s way of doing that.

This is useful for several reasons:

  1. If you have a script needs another script to work (e.g. you have a script which uses jQuery).
  2. If you have a several scripts that require a common setup (e.g. you have 2 scripts that require 1 common variable to be set on the page)
  3. If you require scripts to be run in a precise order (e.g. one script sets up a cookie, that another script needs in order to run)

Setting up dependencies

To set up a dependency for a script, edit the script and open up the dependencies tab down the bottom:

dependencies

The scripts on the right are the scripts that are dependencies. The scripts on the right need to be successfully executed before this script will run.

To set a dependency, select a script on the left hand column and click the > button. You can add all scripts as dependencies with the >> button.

To remove a dependency, select a script on the right hand column and click the < button. You can remove all scripts as dependencies with the << button.

You can observe the status of script loading using the debug tool.

Rule-based dependencies

Dependencies can be used to share filter logic between multiple tags. This is particularly useful when managing larger tag management projects. For more information please see this article in our support center.

A note on filters

Dependencies should be used to load one tag before another, but they cannot be used to load in a library for use in another tag’s filters. The reason for this is that filters run before dependencies.

For example, let’s say you wanted to use a custom filter to fire a tag based upon an element that exists on the page, and use jQuery to check for that element. If jQuery isn’t on the page already, you’ll need to load it in (e.g. from Google’s CDN):

opentag-jquery

To then use jQuery within a filter of a different script you’ll need to poll for the existence of the window.jQuery object in a custom starter filter.

function (session, cb) {
  var waitForJquery = function(callback) {
    if (window.jQuery) {
      callback();
    } else {
      setTimeout(function() {
        waitForJquery(callback);
      }, 100);
    }
  };

  // Wait for window.jQuery to exist
  waitForJquery(function() {

    // Now it does, check for the existence of an element,
    // and fire the tag if we can find it
    if ($("a.mylink").length) {
      cb();
    }

  });  
}

Was this helpful?