I'm fairly new to Drupal, so bare with me. I need to add some tracking code immediately after the body tag on the checkout complete page. I'm guessing I need to add it to the html.tpl.php or $page_top variables or maybe a checkout pane. My question is:

  1. Which way is the best way to do it?
  2. If I need to add it in the html.tpl.php, how would I do that for just the checkout complete page?
  3. If I need to add it in the $page_top variable for only the checkout complete page, how would I do that?
  4. If I need to do it in a checkout pane, how would I keeping in mind is needs to be immediately after the body tag?

Using the Drupal Commerce module.


You can do this in template.php by using theme_preprocess_html()

function mytheme_preprocess_html(&$vars) {
  $arg = arg();
  $vars['checkout_tracking_script'] = '';
  if (count($arg) == 3 && $arg[0] == 'checkout' && $arg[1] == 'complete' && is_numeric($arg[2])) {
    $vars['checkout_tracking_script'] = '<script>myScript</script>';

Then in your html.tpl.php you can print your script like this.

<?php if ($checkout_tracking_script): ?>
  <?php print $checkout_tracking_script; ?>
<?php endif ?>

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.