0

My organization is using Eloqua, and to this point have been using the Eloqua module. The main point of this module is just to determine where the external tracking script should be loaded on the site, and then load the external script while passing it a couple of parameters. This call is creating a huge performance hit, and so after talking to Eloqua support, I copied the external file to the site so it could be cached. Since we need the file to be loaded for every page, we really don't need the module, and so all we really need to do is load the file and pass it the required parameters

The problem is determining how to pass the required parameters to the script. Here is the current eloqua.js:

var _elqQ = _elqQ || [];

(function ($) {

  Drupal.behaviors.eloquaTracking = {
    attach: function (context, settings) {

      _elqQ.push(['elqSetSiteId', Drupal.settings.eloqua.siteId]);
      _elqQ.push(['elqTrackPageView']);

      function async_load() {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = '//img.en25.com/i/elqCfg.min.js';
        var x = document.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
      }
      if (window.addEventListener) {
        window.addEventListener('DOMContentLoaded', async_load, false);
      } else if (window.attachEvent) {
        window.attachEvent('onload', async_load);
      }
    }
  };
})(jQuery);

The _elqQ array is accessed by elqCfg.min.js, so I need to make it accessible to the script with the elqSetSiteId and elqTrackPageView parameters. Is there a way to do that while adding elqCfg.min.js via drupal_add_js() in template.php or somewhere else?

CLARIFICATION: What I'm looking for here is a way to include this file and pass the parameters to it without having to create a whole module. That seems like a lot of unnecessary overhead for this one file.

  • I'm sorry, I've re-read your post a couple of times, but you lost me with everything you wrote after the code block. I can't tell what the problem is, how the scripts relate to each other, or what exactly the problem you are having is. – Jaypan Jun 20 '18 at 23:24
  • Sorry. I have copied the external file to my site, and I just need to 1) load that file on each page load and 2) pass it the two values that are stored in the _elqQ variable in the code above. – wonder95 Jun 20 '18 at 23:42
  • You can load that file on every page load by including it in a module or theme .info file. You can pass the variables by implementing hook_page_alter(), using drupal_add_js() and adding a JS setting. – Jaypan Jun 20 '18 at 23:45
  • Right, I get that. However, just adding it to Drupal.settings doesn't automatically make it available in the _elqQ array, which is what the script is looking for. Do I need to use a behavior as a wrapper to do that? – wonder95 Jun 20 '18 at 23:47
  • 1
    No, the way you have declared it makes it global. Make sure this script comes before the minified script you reference, and the _elqQ variable will already be accessible by your minified script. – Jaypan Jun 20 '18 at 23:52
0

I ended up just going with a modification of the current code. I modified the behavior above so that it just loads the variables:

var _elqQ = _elqQ || [];

(function ($) {

  Drupal.behaviors.eloquaTracking = {
    attach: function (context, settings) {

      _elqQ.push(['elqSetSiteId', Drupal.settings.eloqua.siteId]);
      _elqQ.push(['elqTrackPageView']);
    }
  };
})(jQuery);

and then used drupal_add_js() to load the tracking file in my module

drupal_add_js(drupal_get_path('module', 'eloqua') . '/elqCfg.min.js');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.