Last active
November 9, 2023 20:20
-
-
Save megclaypool/11080bb692c550e35eb7364f7f6d844b to your computer and use it in GitHub Desktop.
[Load code into head of Drupal site] On NIJC the client wanted to load a hotjar script into the head of the document (which then calls another script and attaches it to the head). However, they only wanted the script to load on certain pages. Here's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* implements hook_page_attachments_alter | |
* @param array $attachments | |
*/ | |
//thanks: https://www.drupal.org/theme-guide/8/assets#configurable-javascript | |
function gavias_tico_subtheme_page_attachments_alter(array &$attachments) { | |
/** | |
* Hotjar tracking code for NIJC | |
*/ | |
// Get the page alias of the current page | |
$current_path = \Drupal::service('path.current')->getPath(); | |
$result = \Drupal::service('path_alias.manager')->getAliasByPath($current_path); | |
// https://immigrantjustice.org | |
// https://immigrantjustice.org/es | |
// https://immigrantjustice.org/es/node/2956 | |
// https://immigrantjustice.org/for-attorneys | |
// https://immigrantjustice.org/immigrants | |
// https://immigrantjustice.org/issues | |
// https://immigrantjustice.org/for-attorneys/legal-resources | |
$list = ['/home', '/es', '/es/home', '/es/node/2956', '/for-attorneys', '/immigrants', '/issues', '/for-attorneys/legal-resources']; | |
// Get the language codes, so we can target specific language versions of pages. | |
function get_lang() { | |
$languagecode = \Drupal::languageManager()->getCurrentLanguage()->getId(); | |
$default_languagecode = \Drupal::languageManager()->getDefaultLanguage()->getId(); | |
if ($languagecode == $default_languagecode) { | |
return ""; | |
} else { | |
return "/" . $languagecode; | |
} | |
} | |
// If we're on one of the pages in the list, add the hotjar script to the head. | |
if (in_array(get_lang() . $result, $list)) { | |
$attachments['#attached']['html_head'][] = [ | |
// The data. | |
[ | |
'#type' => 'html_tag', | |
// The HTML tag to add, in this case a tag. | |
'#tag' => 'script', | |
// The value of the HTML tag, here we want to end up with alert("Hello world!");. | |
'#value' => "<!-- Hotjar Tracking Code for http://www.immigrantjustice.org -->" | |
. "(function(h,o,t,j,a,r){" | |
. "h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};" | |
. "h._hjSettings={hjid:312191,hjsv:6};" | |
. "a=o.getElementsByTagName('head')[0];" | |
. "r=o.createElement('script');r.async=1;" | |
. "r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;" | |
. "a.appendChild(r);" | |
. "})(window,document,'//static.hotjar.com/c/hotjar-','.js?sv=');", | |
// set attributes like src to load a file | |
// '#attributes' => array('src' => ''), | |
], | |
// A key, to make it possible to recognize this HTML element when altering. | |
'hotjar' | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment