Skip to content

Commit

Permalink
Adding Pendo integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash committed Nov 10, 2019
1 parent 4a83d46 commit d4c080e
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/df/df_tools/acquia_pendo/acquia_pendo.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'Acquia Pendo'
type: module
description: 'Pendo integration for Acquia products.'
core: 8.x
package: 'Web Services'
27 changes: 27 additions & 0 deletions modules/df/df_tools/acquia_pendo/acquia_pendo.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @file
* Contains acquia_pendo.module.
*/

/**
* Implements hook_preprocess_HOOK().
*/
function acquia_pendo_preprocess_html(&$variables) {
// TEST: putenv('AH_SITE_GROUP=eemgrasmick');.
$acquia_env_vars = [
'AH_CURRENT_REGION',
'AH_SITE_NAME',
'AH_APPLICATION_UUID',
'AH_REALM',
'AH_NON_PRODUCTION',
'AH_SITE_ENVIRONMENT',
'AH_SITE_GROUP',
];
foreach ($acquia_env_vars as $value) {
if (getenv($value) !== FALSE) {
$variables['#attached']['drupalSettings']['pendo']['data']['account'][$value] = getenv($value);
}
}
}
13 changes: 13 additions & 0 deletions modules/df/df_tools/pendo/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "drupal/pendo",
"type": "drupal-module",
"description": "Provides integration with Pendo software.",
"keywords": [
],
"homepage": "https://www.drupal.org/project/pendo",
"minimum-stability": "dev",
"support": {
"issues": "https://www.drupal.org/project/issues/pendo",
"source": "http://cgit.drupalcode.org/pendo"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api_key: ''
21 changes: 21 additions & 0 deletions modules/df/df_tools/pendo/js/pendo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.pendo = {
attach: function (context, settings) {

// Immediately-invoked function expression to prevent scope pollution...
(function(apiKey){ // <-- passed in at the bottom
(function(p,e,n,d,o){var v,w,x,y,z;o=p[d]=p[d]||{};o._q=[];
v=['initialize','identify','updateOptions','pageLoad'];for(w=0,x=v.length;w<x;++w)(function(m){
o[m]=o[m]||function(){o._q[m===v[0]?'unshift':'push']([m].concat([].slice.call(arguments,0)));};})(v[w]);
// Create a `<script>` tag linked to your application-specific Agent build from our CDN...
y=e.createElement(n);y.async=!0;y.src='https://cdn.pendo.io/agent/static/'+apiKey+'/pendo.js';
z=e.getElementsByTagName(n)[0];z.parentNode.insertBefore(y,z);
})(window,document,'script','pendo');

// Call this whenever information about your visitors becomes available
// Please use Strings, Numbers, or Bools for value types.
pendo.initialize(drupalSettings.pendo.data);
})(drupalSettings.pendo.api_key); //
}
};
})(jQuery, Drupal, drupalSettings);
5 changes: 5 additions & 0 deletions modules/df/df_tools/pendo/pendo.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'Pendo'
type: module
description: 'Provides integration with Pendo software.'
core: 8.x
package: 'Web Services'
6 changes: 6 additions & 0 deletions modules/df/df_tools/pendo/pendo.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pendo-js:
version: 1.x
js:
js/pendo.js: {}
dependencies:
- core/drupalSettings
6 changes: 6 additions & 0 deletions modules/df/df_tools/pendo/pendo.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pendo.pendo_admin_form:
title: 'Pendo'
route_name: pendo.pendo_admin_form
description: 'Edit Pendo configuration.'
parent: system.admin_config_services
weight: 99
19 changes: 19 additions & 0 deletions modules/df/df_tools/pendo/pendo.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* Contains pendo.module.
*/

use Drupal\user\Entity\User;

/**
* Implements hook_preprocess_HOOK().
*/
function pendo_preprocess_html(&$variables) {
$variables['#attached']['library'][] = 'pendo/pendo-js';
$variables['#attached']['drupalSettings']['pendo']['api_key'] = \Drupal::config('pendo.settings')->get('api_key');
$variables['#attached']['drupalSettings']['pendo']['data']['account']['id'] = \Drupal::config('system.site')->get('uuid');
$variables['#attached']['drupalSettings']['pendo']['data']['visitor']['id'] = User::load(\Drupal::currentUser()->id())->uuid();
$variables['#attached']['drupalSettings']['pendo']['data']['visitor']['email'] = \Drupal::currentUser()->getEmail();
}
9 changes: 9 additions & 0 deletions modules/df/df_tools/pendo/pendo.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pendo.pendo_admin_form:
path: '/admin/config/services/pendo'
defaults:
_form: '\Drupal\pendo\Form\PendoAdminForm'
_title: 'PendoAdminForm'
requirements:
_permission: 'access administration pages'
options:
_admin_route: TRUE
94 changes: 94 additions & 0 deletions modules/df/df_tools/pendo/src/Form/PendoAdminForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Drupal\pendo\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class PendoAdminForm.
*/
class PendoAdminForm extends ConfigFormBase {

/**
* Drupal\Core\Config\ConfigFactoryInterface definition.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a new PendoAdminForm object.
*/
public function __construct(
ConfigFactoryInterface $config_factory
) {
parent::__construct($config_factory);
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'pendo.settings',
];
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'pendo_admin_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('pendo.settings');

// @see https://www.drupal.org/docs/7/api/localization-api/dynamic-or-static-links-and-html-in-translatable-strings
$form['link'] = [
'#markup' => $this->t('Pendo links: <a href="@pendo-admin" target="_blank">Admin Settings</a><br /><br />', [
'@pendo-admin' => Url::fromUri('https://app.pendo.io/admin', ['attributes' => ['target' => '_blank']])->toString(),
]),
];
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('Pendo API Key'),
'#description' => $this->t('This key will be visible to the public in the HTML source code and is not intended to be secret.'),
'#default_value' => $config->get('api_key'),
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

$this->config('pendo.settings')
->set('api_key', $form_state->getValue('api_key'))
->save();

$this->messenger()->addMessage("You may need to clear caches for your new API key to correctly load in page markup.", MessengerInterface::TYPE_STATUS);
}

}
46 changes: 46 additions & 0 deletions modules/df/df_tools/pendo/tests/src/Functional/LoadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\Tests\pendo\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;

/**
* Simple test to ensure that main page loads with module enabled.
*
* @group pendo
*/
class LoadTest extends BrowserTestBase {

/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['pendo'];

/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}

/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->statusCodeEquals(200);
}

}

0 comments on commit d4c080e

Please sign in to comment.