CuBoulder / ucb2021_base

Drupal 9 Base theme for CU Boulder web properties
GNU General Public License v2.0
0 stars 1 forks source link

Helpscout Integration #156

Closed kevincrafts closed 1 year ago

kevincrafts commented 2 years ago

We use Helpscout to provide in-site documentation and an entry into our ticketing system. We use the code below to add the Helpscout beacon when users are logged in and have permission to access the admin navbar.

The beacon is hidden by default, but there is a line of jQuery javascript that opens the beacon when a user clicks on a specific link in the navbar. The beacon also collects the user's information so if they submit the form, the user information is in the ticket.

function cu_helpscout_preprocess_page(&$vars) { if (user_access('access navbar')) {

// Information to attach to beacon form
global $user;
global $base_url;
$name = realname_load($user);
$roles = join(', ', $user->roles);
$site_name = variable_get('site_name', '');
$site_name = str_replace(' ', '-', $site_name);
$site_name = preg_replace('/[^A-Za-z0-9\-]/', '', $site_name);

$beacon = '!function(e,t,n){function a(){var e=t.getElementsByTagName("script")[0],n=t.createElement("script");n.type="text/javascript",n.async=!0,n.src="https://beacon-v2.helpscout.net",e.parentNode.insertBefore(n,e)}if(e.Beacon=n=function(t,n,a){e.Beacon.readyQueue.push({method:t,options:n,data:a})},n.readyQueue=[],"complete"===t.readyState)return a();e.attachEvent?e.attachEvent("onload",a):e.addEventListener("load",a,!1)}(window,document,window.Beacon||function(){}); window.Beacon("init", "1bb01225-0287-4443-a7e5-be8f375a7766");';
// Beacon Prefill.
$config = 'Beacon(\'prefill\', {
  name: \'' . $name . '\',
    email: \'' . $user->mail . '\',
    user_email: \'' . $user->mail . '\',
    roles: \'' . $roles . '\',
    site_name: \'' . $site_name . '\',
    site_url: \'' . $base_url . '\',
});';
// Beacon config.
$config .= 'Beacon(\'config\', {
  \'display\': {
    \'style\': \'manual\',
    \'iconImage\': \'message\'
  }
});
jQuery("#navbar-bar a.navbar-icon-help").click(function(event) { event.preventDefault(); Beacon(\'open\'); });';

drupal_add_js($beacon, array(
  'type' => 'inline',
  'scope' => 'footer',
  'weight' => 100,
));

drupal_add_js($config, array(
  'type' => 'inline',
  'scope' => 'footer',
  'weight' => 101,
));
drupal_add_css('#hs-beacon iframe {background:rgba(0,0,0,.75) !important; }', 'inline');

}