Skip to content

Instantly share code, notes, and snippets.

@thewheat
Created June 5, 2017 15:23
Show Gist options
  • Save thewheat/c7306fa2d1790eb3e03128ff3fd67115 to your computer and use it in GitHub Desktop.
Save thewheat/c7306fa2d1790eb3e03128ff3fd67115 to your computer and use it in GitHub Desktop.
Sample Drupal 8 installation for Intercom. This is a very rough way of how to install Intercom in Drupal (as I'm not too familiar with Drupal in general). Requires an existing theme in Drupal so replace "themename" in the following files with the actual theme being used
// create this file js/intercom.js
(function ($, Drupal, drupalSettings) {
'use strict';
Drupal.behaviors.attach_intercom = {
attach: function (context, settings) {
window.intercomSettings = drupalSettings.intercom;
// log out of Intercom when logout button clicked
jQuery("body").on("click", "a[data-drupal-link-system-path='user/logout']", function(){Intercom("shutdown"); });
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function") {ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/' + drupalSettings.intercom.app_id;var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()
}
};
})(jQuery, Drupal, drupalSettings);
# add this to existing file
intercom:
js:
js/intercom.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
////////////////////////////////////////////////////////////////////////////////////////////////
// Intercom integration
////////////////////////////////////////////////////////////////////////////////////////////////
// - Supports logged out users / visitors
// - Supports logged in users and has logout as well (NOTE: customise logout behaviour based on your theme)
//----------------------------------------------------------------------------------------------
// IMPORTANT
// - Customise logout behaviour based on your theme (used "bartik" theme on a standard install of Drupal 8.3.2)
// - Not too familiar with PHP santization with Drupal but this Unsure how best to santize PHP code so using addslashes in this instance. Do comment if there is a better way (did not want to use htmlentities as would want to keep raw values)
//----------------------------------------------------------------------------------------------
// - Doc resources
// - Logged out snippet: https://docs.intercom.com/install-on-your-product-or-site/quick-install/install-intercom-on-your-website-for-logged-out-visitors
// - Logged in snippet: https://docs.intercom.io/install-on-your-product-or-site/install-intercom-on-your-web-app
// - Logging out: https://docs.intercom.com/faqs-and-troubleshooting/your-users-and-leads-data-in-intercom/how-do-i-end-a-session
// - Add any other custom attributes as you see fit https://docs.intercom.com/configure-intercom-for-your-product-or-site/customize-intercom-to-be-about-your-users/send-custom-user-attributes-to-intercom
////////////////////////////////////////////////////////////////////////////////////////////////
function themename_page_attachments_alter(&$page) {
$page['#attached']['library'][] = 'themename/intercom';
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
// configuration
$page['#attached']['drupalSettings']['intercom']['app_id'] = "YOUR_APP_ID";
if(!$user->isAnonymous()){
$page['#attached']['drupalSettings']['intercom']['user_id'] = $user->get('uid')->value;
// for identity verification https://docs.intercom.com/configure-intercom-for-your-product-or-site/staying-secure/enable-identity-verification-on-your-web-product
$page['#attached']['drupalSettings']['intercom']['user_hash'] = hash_hmac('sha256', $user->get('uid')->value, "YOUR_SECRET_KEY");
$page['#attached']['drupalSettings']['intercom']['email'] = $user->get('mail')->value;
$page['#attached']['drupalSettings']['intercom']['name'] = $user->get('name')->value;
$page['#attached']['drupalSettings']['intercom']['created_at'] = $user->get('created')->value;
}
}
@thewheat
Copy link
Author

thewheat commented Jun 5, 2017

Illustrating file layout and values (used "bartik" theme on a standard install of Drupal 8.3.2)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment