1

I'm looking for the best way (and also simplest if different) to accomplish this in Drupal 7.

  • Users arriving at example.com/special-redirect should be redirected quickly to example.com/newpage

  • Nothing needs to be rendered, but before they are redirected to the new URL on our site, I need to make sure our JS tracking script is executed.

  • the tracking script is currently loaded into the header from the theme.info file, and needs to run on every page of the site.

I assume that the URL redirect module won't accomplish this (ie. never loads the page).

I'm thinking I have to rely on the javascript method of putting this somewhere:

   <script type="text/javascript">
        window.location = "http://www.yoururl.com";
   </script>

But I'm not sure where I should put this (or a modified one) on the site. It's just for the one page, but would putting it in the html.tpl.php file with and if statement for the NodeID work? I'm also open to moving the tracking script if it makes sense to.

thanks for any advice

1 Answer 1

2

You can do something like below:

  1. Create a custom module, If not already created (read more).
  2. In your custom module, define a custom callback using hook_menu, and add a page callback.
  3. In page callback function simply 'echo "script file/code"'. This will prevent Drupal from rendering various template files in your callback. NOTE:* If you use return instead of echo, Drupal will start rendering template files.
  4. In your .JS file/code, do required tracking and re-direct user to another page.

Sample Code:

MODULE_NAME.module

/**
 * Implements hook_menu().
 */
function [MODULE_NAME]_menu() {
  $items['custom_url'] = array(
    'title' => 'Test',
    'page callback' => '_[MODULE_NAME]_callback',
    // Check required access.
    'access callback' => TRUE,
  );

  return $items;
}

function _[MODULE_NAME]_callback() {
  $jquery = base_path() . 'misc/jquery.js';
  $script = drupal_get_path('module', 'MODULE_NAME') . '/[MODULE_NAME].js';
  echo '<script src="' . $jquery . '"></script>
    <script src="' . $script . '"></script>';
}

MODULE_NAME.js

(function() {
  $(document).ready(function() {
    // Do your tracking here.
    console.log("Test");
    window.location.href = "http://example.com";
  });
}(jQuery));

Edit :

No worries, if you've not created a module before, you can follow steps given in How to Make a Simple Module with a Form and Menu Link to try how it works. The documentation is regarding creating a module which will define a menu callback and form.

Here you can find a detailed guide regarding Creating custom modules.

The code snippet I've provided above includes all basic code as per your requirement, except the tracking code which you need to write inside MODULE_NAME.js file. See the comment saying // Do your tracking here.

4
  • Thanks Yogesh. I haven't built a module before, but seems like a good time to try. Is the tracking code included here in the module because the main one (as directed in the theme.info file) won't have a chance to execute? As I wouldn't want it to execute twice of course.
    – brian
    Nov 21, 2016 at 14:01
  • @brian that's correct - you're not loading drupal's template stack here at all. You're using drupal to provide what I would term a pseudo-page it's nowhere near a complete page by drupal's standards. Nov 21, 2016 at 15:25
  • @brian, No worries, I've updated answer with reference to module creation documentation. You can refer that. Regarding tracking code, you will need to write that, because it will be based on your business logic.
    – Yogesh
    Nov 22, 2016 at 4:42
  • Got it all working good. Thanks again. I actually did use that very link you put, so it's great to have it here for others. And gotcha re: tracking code, I hadn't quite understood the full scope of what this module was doing so I was worried about the tracking code executing twice, but got it now thnx to additional explanation bt @HomoTechsual .
    – brian
    Nov 22, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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