We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44661
    • 31 Posts
    Hi,
    Using GPT JS code for responsive tags, for example :
    <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
    <script>
      var googletag = googletag || {};
      googletag.cmd = googletag.cmd || [];
    </script>
    
    <script>
      googletag.cmd.push(function() {
      var mapping = googletag.sizeMapping().
    	addSize([1280, 0], [[728, 90], [468, 60]]). 
    	addSize([980, 0], [468, 60]).
    	addSize([600, 0], [320, 50]). 
    	build();
    	
       googletag.defineSlot('/12345678/OC3-lbgc1', [[728, 90], [320, 50], [468, 60]], 'div-gpt-ad-12345678901234-0').
    	defineSizeMapping(mapping).
    	addService(googletag.pubads());
        googletag.pubads().enableSingleRequest();
        googletag.enableServices();
      });
    </script>


    I have a problem whenever there is a [[ in the code (ex , [[728, 90], [320, 50], [468, 60]],) since i guess MODX processes it and it is not rendered.

    Is there a way to prevent Modx from parsing this JS code ?

    I found a first fix using such a snippet :
    <?php
    $pubid =  (int) $modx->getOption('pubid', $scriptProperties);
    $resource = $modx->getObject('modResource',$pubid);
    $output= $resource->get('content');
    
    return $modx->regClientStartupScript($output);


    when placed in a resource content to get the JS script above the </head> tag.

    However when using for instance :
    <?php
    $pubid =  (int) $modx->getOption('pubid', $scriptProperties);
    $resource = $modx->getObject('modResource',$pubid);
    $output= $resource->get('content');
    
    return $output;
    to use it in a template (pubid being the id# of the JS resource with the appropriate Google JS code) it
    doesn't work !
    I get
       googletag.defineSlot('/12345678/OC3-lbgc1', , 'div-gpt-ad-12345678901234-0').

    instead of
       googletag.defineSlot('/12345678/OC3-lbgc1', [[728, 90], [320, 50], [468, 60]], 'div-gpt-ad-12345678901234-0').

    so the Google code is broken.

    Any idea ?
    Thanks !
      • 3749
      • 24,544 Posts
      You can try wrapping the script in CDATA tags.

      It should also work to put a space between the leading pair of square brackets:

      [ [728, 90]


      MODX then won't view them as the start of a MODX tag. You can leave the trailing ones alone.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 44661
        • 31 Posts
        Hi Bob and thanks for your feedback
        Adding a space works with Modx but seems to break the GPT code (not so easy to tell because these are screen dimensions options which are not always used depending upon screen, ad availability, etc)

        As i would prefer not to modify anything in the Google code (it's actually their GPT rules) i was looking for solutions on the Modx side.
        For instance with
        return $modx->regClientStartupScript($output);
        (my first a.m. snippet) the JS code is not amended

        which is not the case with
         return $output= $resource->get('content');
        where the JS code is parsed (used in a chunk in template)

        Is there for instance another Modx function behaving like regClientStartupScript ?
        Thanks
          • 3749
          • 24,544 Posts
          There is regClientStartupHTMLBlock(). Check out the "See Also" section on that page for other options.

          I'm not sure this is relevant for your issue but for the MODX regClient script functions, if your code contains script tags, it will be injected unaltered. If not, MODX will add the script tags itself.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting