DTM Examples- Setting a global "s object

At this point in time, by default, DTM creates your analytics object (usually an "s", as in "s.pageName") with a local scope. This means it should be able to be referenced from any custom code blocks within DTM that are tied to your analytics tool. However, it would NOT be accessible from code on the page (or a developer console) or even non-analytics code blocks in DTM (like Third Party Tags). This can cause some pretty big problems if you aren't aware.


To get around this, you need to define your own "s" object within your library. This does mean you can't let DTM manage your library, but the change you need to make is pretty minor. You need a "Custom" configuration, and you'll need to "Set report suites using custom code below" (since you're essentially going to be overwriting the "s" object that DTM created, where it set your report suites for you.


When you open the editor, add this code to the top:

var s_account=''
var s=s_gi(s_account)

Make sure to replace the "myDevSuite" and "myProdSuite" with the correct report suites- these should match what you have in the interface. This uses _satellite.settings.isStaging to detect the current library and set the appropriate s_code.

Alternatively, if you still want to let DTM set your report suite based on whether you're in the staging or production library, you can use this code:

function getAnalyticsAccount() {
  for(var toolid in _satellite.tools) {
    if(_satellite.tools[toolid].settings.engine == "sc") {
      return _satellite.tools[toolid].settings.account;
var s_account=getAnalyticsAccount();
var s=s_gi(s_account)

With that in place, you should be able to access the "s" object from anywhere in DTM or on your page.