Skip to content

Commit

Permalink
#328: add flourish analytics to pages with embeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
komejo committed Jan 8, 2025
1 parent 63ba976 commit 39bb27c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
62 changes: 62 additions & 0 deletions modules/wri_seo/js/flourish_analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Ensure the Flourish global object exists before loading the script.
if (typeof window.Flourish === "undefined") {
window.Flourish = {};
}

// Function to add the analytics listener when Flourish is ready.
function addAnalyticsListener() {
if (typeof window.Flourish.addAnalyticsListener === "function") {
window.Flourish.addAnalyticsListener(function (event) {
var action = event.action;

var category = "flourish_engagement";
var label = event.story_id
? "Flourish story " + event.story_id
: "Flourish visualization " + event.visualisation_id;

var dataLayerEvent = {
event: action,
event_category: category,
event_label: label,
event_action: action,
};

// Include additional data from the event object.
Object.keys(event).forEach(function (key) {
if (key !== "action") {
dataLayerEvent[key] = event[key];
}
});

// Push the event to the dataLayer.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(dataLayerEvent);
});
} else {
console.warn("Flourish.addAnalyticsListener is not available.");
}
}

// Wait for DOMContentLoaded to ensure Flourish script has run.
document.addEventListener("DOMContentLoaded", function () {
// Add the analytics listener after the embed script has initialized.
if (typeof window.Flourish.addAnalyticsListener === "function") {
addAnalyticsListener();
} else {
console.warn(
"Flourish.addAnalyticsListener is not available after DOMContentLoaded."
);
}
});

// Dynamically load the Flourish embed script if not already loaded.
if (!document.querySelector('script[src="https://public.flourish.studio/resources/embed.js"]')) {
var script = document.createElement('script');
script.src = "https://public.flourish.studio/resources/embed.js";
script.async = true;
script.onload = function () {
// Add the listener if Flourish is ready after script load.
addAnalyticsListener();
};
document.head.appendChild(script);
}
4 changes: 4 additions & 0 deletions modules/wri_seo/wri_seo.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ wri-seo:
dependencies:
- jquery_once/jquery
- core/drupalSettings
flourish_analytics:
version: 1.x
js:
flourish_analytics.js: { type: file, scope: header, weight: 0 }
39 changes: 39 additions & 0 deletions modules/wri_seo/wri_seo.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @file
* WRI SEO related hooks.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;

/**
* Implements hook_page_attachments().
Expand Down Expand Up @@ -45,6 +46,44 @@ function wri_seo_page_attachments(array &$attachments) {
break;

}

// Check for Flourish embeds.
if ($node->hasField('body') && !$node->get('body')->isEmpty()) {
$body = $node->get('body')->value;
preg_match_all('/<drupal-media.*?data-entity-uuid="(.*?)".*?>/', $body, $matches);

if (!empty($matches[1])) {
foreach ($matches[1] as $uuid) {
// Load the media entity by UUID.
$media = \Drupal::service('entity.repository')->loadEntityByUuid('media', $uuid);

if ($media && $media->bundle() === 'embed') {
$source = $media->get('field_media_embed_code')->value;

if (strpos($source, 'flourish-embed') !== FALSE) {
// $attachments['#attached']['library'][] = 'wri_seo/flourish_analytics';
$module_path = \Drupal::service('extension.path.resolver')->getPath('module', 'wri_seo');

// Add custom Flourish analytics script inline.
$script_path = $module_path . '/js/flourish_analytics.js';
if (file_exists($script_path)) {
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => ['type' => 'text/javascript'],
'#value' => file_get_contents($script_path),
],
'flourish_analytics',
];
} else {
\Drupal::logger('wri_seo')->warning('Flourish analytics script not found at @path', ['@path' => $script_path]);
}
break;
}
}
}
}
}
}

// Exclude certain paths from crawlers per WRIN issue #323.
Expand Down

0 comments on commit 39bb27c

Please sign in to comment.