We have a client that uses subdomains and needs to add a custom _udn="website.com" variable *before* the call to urchinTracker(). Can you add a textarea (sort of like the "JavaScript Code" area under "Advanced") that allows various Urchin-related vars to be declared between _uacct = "UA-#####-#" and urchinTracker()?

Thanks so much for building this. It's really useful.

CommentFileSizeAuthor
#3 i180935.patch1.95 KBtodd nienkerk

Comments

todd nienkerk’s picture

I edited my local copy of the module to do this.

First change: Add new textarea to hook_admin_settings()

// CUSTOM 2007-10-03 TRN
  $form['advanced']['googleanalytics_variables'] = array(
    '#type' => 'textarea',
    '#title' => t('Urchin Variables'),
    '#default_value' => variable_get('googleanalytics_variables', ''),
    '#rows' => 10,
    '#description' => t('Paste custom Urchin variables here. These will appear before the call to urchinTracker() and will be added to every page that Google Analytics appears on. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).')
  );
// END CUSTOM

This was added immediately above this line: $form['advanced']['googleanalytics_codesnippet'] = array(

Second change: Pull the variable in hook_footer()

// CUSTOM 2007-10-03 TRN
		// Add any custom variables if specified
		$variables = variable_get('googleanalytics_variables', '');
// END CUSTOM

This was added immediately below this line: $codesnippet = variable_get('googleanalytics_codesnippet', '');

Third (and final) change: Output the variables in hook_footer

// CUSTOM 2007-10-03 TRN
    $script .= '<script type="text/javascript">'. $downloadtracker ."\n_uacct = \"". $id ."\";{$variables}urchinTracker();{$segmentation}{$codesnippet}\n</script>\n";
// END CUSTOM

This replaced the following line: $script .= '<script type="text/javascript">'. $downloadtracker ."\n_uacct = \"". $id ."\";urchinTracker();{$segmentation}{$codesnippet}\n</script>\n";

hass’s picture

please provide a patch. thx

todd nienkerk’s picture

Version: 5.x-1.3 » 5.x-1.x-dev
Assigned: Unassigned » todd nienkerk
Status: Active » Needs review
StatusFileSize
new1.95 KB

I've created a patch to add my changes to the dev version of the module. :)

hass’s picture

Do you have examples what we can add as custom variables here?

todd nienkerk’s picture

Here's a variable that allows Google Analytics to track several subdomains within one profile:

_udn="mydomain.com";

Here's a more detailed explanation of _udn, and here's a list of some other variables you can use.

hass’s picture

Status: Needs review » Needs work

Thank you for this links, saved some time.

I would say your patch doesn't fit the real need here. Well it will work for your domain variable, but as you can see on your linked page there are many params that need to be set dynamically per page. You patch works only on global level and will therefor not work for other needs. The idea about adding this additional params is very good and we should think about a better API or hook way. A hook will allow other modules to set their variables. For e.g. Adwords may add their setting and e-commerce module can set their own things...

todd nienkerk’s picture

Can a new version of Google Analytics at least support global variables prior to the urchinTracker() call? I realize it won't suit absolutely every need, but it's a step in the right direction.

hass’s picture

As i remember from core comments - we should avoid using global variables wherever possible. Today I'm thinking more of a hook_google_analytics with an array of settings... i will try to build an first version and then we see if this would be good practice.

Maybe budda could comment on this if he know about a better way, i haven't thought about yet...

todd nienkerk’s picture

I think we may be misunderstanding each other with regards to "global variables." I'm not advocating creating a Drupal-wide variable -- I'm simply saying it would be useful to have a way to set additional Google Analytics variables prior to the urchinTracker() call. This is absolutely necessary for sites with several subdomains whose traffic should be monitored at the primary domain level.

While creating a hook for other modules is probably useful (I haven't thought about it myself), the ability to add one or two site-wide Google Analytics variables seems like a useful way to enhance the module -- especially for users who don't know how to work with modules or hooks.

The client I'm working with, for example, had no interest in using the Google Analytics module until I made the changes that allowed him to add a variable to the output. Now he has an easy way to add them: just a simple text area.

akrywko’s picture

I have the same request.

This is also necessary for monitoring additional search engines.
For example:
_uOsr[_uOsr.length]="onet.pl"; _uOkw[_uOkw.length]="qt";
_uOsr[_uOsr.length]="wp.pl"; _uOkw[_uOkw.length]="szukaj";

rconstantine’s picture

Another alternative would be to store all of the code, from this line:
$script .= '<script type="text/javascript">'. $downloadtracker ."\n_uacct = \"". $id ."\";urchinTracker();{$segmentation}{$codesnippet}\n</script>\n"; (distilled of course) to the variables table. This would allow it to be fully customized. It could come with what it has now as a default. Either the README could tell us what to put, or the install function could insert the variable into the table.

Another reason this would be good is that if a setting was also made to select between urchin.js and ga.js, then the code could all be changed easily to match. I have changed my copy to work with ga.js. Still testing. I did not implement the changes I'm suggesting.

For those interested, change the above-mentioned line to:
$script .= '<script type="text/javascript">'. $downloadtracker ."\nvar pageTracker = _gat._getTracker(\"". $id ."\");pageTracker._setDomainName(\"yourdomain.com\");pageTracker._initData();pageTracker._trackPageview();{$segmentation}{$codesnippet}\n</script>\n";
where yourdomain is your domain. I think you leave off the subdomain. See http://www.epikone.com/blog/2007/11/19/tracking-sub-domains-with-google-... for an article on tracking all subdomains with one profile in a more flexible way than the old way.

rconstantine’s picture

Oh, I forgot to say that you would also need to change references to urchin.js to point to ga.js.

hass’s picture

Status: Needs work » Fixed

Use $codesnippet for adding static variables to GA, please. The order have been changed in latest D5 and D6 code. Dynamic variables could be added in the way budda described here http://drupal.org/node/142514#comment-552592.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.