Hi,

I installed & set up this module but I could not find how to solve one problem.
I am not sure is there any option how to do this or this should be feature request.

The problem is:

I need to place Google Analytics code in the header and every other JS in footer.
Google Analytics code should be in header because I can not verify Webmaster tools if it is in footer.

CommentFileSizeAuthor
#5 advagg-2898140-GA-in-header.patch583 bytesmikeytown2
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojan_m created an issue. See original summary.

mikeytown2’s picture

I'll add a settings for that in the future. Until then you can use the 'scope_lock' key to keep it in the header. I didn't know about this; good to know: https://support.google.com/webmasters/answer/35179?hl=en#google_analytic...

function hook_js_alter(&$js) {
  foreach ($js as $key => $value) {
    if ($value['type'] === 'inline' && strpos($value['data'], '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date()') !== FALSE) {
      $js[$key]['scope_lock'] = TRUE;
    }
  }
}
bojan.m’s picture

I tested and everything works great.

Thanks.

  • mikeytown2 committed 61efaae on 7.x-2.x
    Issue #2898140: Google Analytics in header & every other JS in footer
    
mikeytown2’s picture

Status: Active » Fixed
FileSize
583 bytes

  • mikeytown2 committed f13d88d on 7.x-2.x
    Issue #2898140: Google Analytics in header & every other JS in footer
    

Status: Fixed » Closed (fixed)

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

dalehgeist’s picture

Was this fix rolled into 7.x-2.30 (the version I'm using)? Google is complaining that my GA tracking code is not in the header (onlinedriversed.com).

mikeytown2’s picture

Yeah this was rolled in on Aug 10th 2017. Are you using https://www.drupal.org/project/google_analytics ?

dalehgeist’s picture

Looks like I (actually whoever built this site) did not use the Drupal module, just added the code snippet. I ended up working around this, but if I encounter it in the future I'll use the GA module. Thanks!