diff --git a/modules/wri_seo/js/flourish_analytics.js b/modules/wri_seo/js/flourish_analytics.js new file mode 100644 index 000000000..7db78e3a9 --- /dev/null +++ b/modules/wri_seo/js/flourish_analytics.js @@ -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); +} diff --git a/modules/wri_seo/wri_seo.libraries.yml b/modules/wri_seo/wri_seo.libraries.yml index d02298e69..01580791a 100644 --- a/modules/wri_seo/wri_seo.libraries.yml +++ b/modules/wri_seo/wri_seo.libraries.yml @@ -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 } diff --git a/modules/wri_seo/wri_seo.module b/modules/wri_seo/wri_seo.module index f79ea0a12..a2018be2d 100644 --- a/modules/wri_seo/wri_seo.module +++ b/modules/wri_seo/wri_seo.module @@ -4,6 +4,7 @@ * @file * WRI SEO related hooks. */ +use Drupal\Core\Asset\AttachedAssetsInterface; /** * Implements hook_page_attachments(). @@ -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('//', $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.