Currently if you AJAX in content that contains a DART tag, the document.write() will take over the entire page in question so you are left with no content at all - just an ad.

In #1146080: Allow override of document.write in dart.js gansbrest suggests that writeCapture may be a solution to this

Comments

gansbrest’s picture

Yep, you can refresh ads dynamically with writeCapture. As example you can check http://www.fastcompany.com/most-creative-people/2011/wadah-khanfar-al-ja... - when you click between Profile, Process and Inspiration on the top, Ad units will be refreshed.

We use DART + WriteCapture library.

bleen’s picture

gansbrest .. did you have to alter the DART module at all? If so, can you post a patch?

Either way can you give just a bit more detail as to what you did to get WriteCapture to work ... as much detail as you can give would be helpfull

gansbrest’s picture

Sure. I didn't alter dart module. The only thing I changed is that I took out document.write from dart.js (as we discussed in here http://drupal.org/node/1146080), but since you added new writeTags setting, it should work as well.

Here is the overview of what I've done to make ad refreshing along with js on the bottom of the page possible:

1) I moved dart.tpl to the project theme and altered it. The idea is that instead of outputting ad tags right away, we create placeholder for those ad units on the page:

Something like that:

  $adCode = '';
  if ($show_script_tag) {
    $adCode = 'Drupal.DART.tag(\''.$json_tag.'\')';
  } 
  // STORE adCode and tagName in the helper module
  module_invoke('mcp_helper', 'getAdCode', $tag->machinename, $adCode);
<!-- RENDER AD PLACEHOLDER -->
<div <?php print drupal_attributes($attributes); ?> >
  <div class="<?php print $tag->machinename; ?>" ></div>
</div>

###########################

// GET OR STORE ADCODES
function mcp_helper_getAdCode($tagId = '', $adCode = '') {
  static $adsList = array();
  if (!empty($adCode)) {
    // Prevent ad tags from the node body from loading before page.tpl ads to preserve correct order
    if(preg_match('|body_tag|is', $tagId, $pock)) {
      $adsList['body_tags'][$tagId] = $adCode;
    } else {
      $adsList['regular_tags'][$tagId] = $adCode;
    }
  } else {
    return $adsList;
  } 
}

2) The next step is to output stored ad tags on the page, so we can render them. To do that I've used preprocess_page function in the template.php.

  // GET ADS HERE
  $adsList = module_invoke('mcp_helper', 'getAdCode');

  // PERSERVE ADS ORDER (tile) IN HERE
  $adsCodeList = $regularTagsList = $bodyTagsList = array();
  $regularTagsList = _getAdsCodeList($adsList['regular_tags']);
  $bodyTagsList = _getAdsCodeList($adsList['body_tags']);
  $adsCodeList = array_merge($regularTagsList, $bodyTagsList);

  // Output ad codes on the page
  drupal_add_js(array('adsList' => $adsCodeList), 'setting');

  // Render ads
  drupal_add_js('$(document).ready(function() { _renderAds(); });', 'inline');

3) Render ads in JS. We used a little overcomplicated logic to render and refresh ad tags on the page, because of those crazy content scrollers. But here is the general idea:

function _renderAds() {
  var adsObj = Drupal.settings.adsList; // Get ads from setting
  ord = 1000000000 + Math.floor(Math.random() * 900000000); 
  tile = 1; // Reset counter every time we update ads, otherwise it will keep advancing 
  
  // Iterate through ads list and replace placeholders with actual ad tags
  for(var key in adsObj) {
    if(adsObj.hasOwnProperty(key)) {
      // RESPOND TO SOME KIND OF EVENT WHICH SHOULD TRIGGER ADS REFRSHING
      ... find('.' + adsObj[key].adId).empty().writeCapture().html(eval(adsObj[key].adContent));
    }
  }

I think that was about it.. )

bleen’s picture

Version: 7.x-2.x-dev » 6.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new7.32 KB

ok .. this patch was largely stolen from some code used by mofolo. Its a whole lot simpler than the code provided in #3

I'm making the change to 6.x first and will port it to 7.x later

bleen’s picture

StatusFileSize
new7.31 KB

... same patch without whitespace issues

bleen’s picture

StatusFileSize
new7.32 KB

the patch in #4 would kill an ad's "slug" This patch fixes that

bleen’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
Status: Needs review » Needs work

I committed #6 to 6.x-dev ... need to port to 7.x now

bleen’s picture

Status: Needs work » Needs review
StatusFileSize
new4.46 KB

this is the patch for 7.x

bleen’s picture

Status: Needs review » Closed (fixed)

committed to 7.x

gansbrest’s picture

Great, I think I will try it out for a new non trivial project pretty soon. Thanks for your work!