diff --git a/includes/klaviyo.admin.inc b/includes/klaviyo.admin.inc index 85fd004..a2809e5 100644 --- a/includes/klaviyo.admin.inc +++ b/includes/klaviyo.admin.inc @@ -48,7 +48,7 @@ function klaviyo_api_integration_settings() { ); } - $js_settings = variable_get('klaviyo_javascript', array('key_public' => '', 'roles' => array())); + $js_settings = variable_get('klaviyo_javascript', array('public_api_key' => '', 'roles' => array())); $form['klaviyo_javascript'] = array( '#type' => 'fieldset', '#title' => t('Javascript API'), @@ -59,13 +59,13 @@ function klaviyo_api_integration_settings() { '#tree' => TRUE ); - $form['klaviyo_javascript']['key_public'] = array( + $form['klaviyo_javascript']['public_api_key'] = array( '#type' => 'textfield', '#title' => t('Public API key'), '#description' => t('The javascript will not be added if the public key is missing. The public key can be retrieved from Account >> Settings >> API Keys from within Klaviyo.', array('!url' => 'https://www.klaviyo.com/account#api-keys-tab') ), - '#default_value' => $js_settings['key_public'], + '#default_value' => $js_settings['public_api_key'], '#size' => 60, '#maxlength' => 128, '#required' => FALSE, diff --git a/klaviyo.js b/klaviyo.js index 22d86ed..6e80753 100644 --- a/klaviyo.js +++ b/klaviyo.js @@ -3,26 +3,27 @@ * Provides JavaScript for Klaviyo. */ +// Must be in the global namespace so that the Klaviyo 3rd party script may +// access this variable. +var _learnq = _learnq || []; + (function ($) { Drupal.behaviors.klaviyo = { attach: function (context) { - var identity = Drupal.settings.klaviyo.identify.$email - var _learnq = _learnq || []; + var klaviyo = Drupal.settings.klaviyo || {} - _learnq.push(['account', Drupal.settings.klaviyo.key_public]); + if (klaviyo.public_api_key) { + _learnq.push(['account', klaviyo.public_api_key]); + } - if (identity) { - _learnq.push(['identify', { - '$email' : identity - }]); + if (klaviyo.identify) { + _learnq.push(['identify', klaviyo.identify]); }; - (function () { - var b = document.createElement('script'); b.type = 'text/javascript'; b.async = true; - b.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'a.klaviyo.com/media/js/analytics/analytics.js'; - var a = document.getElementsByTagName('script')[0]; a.parentNode.insertBefore(b, a); - })(); + var b = document.createElement('script'); b.type = 'text/javascript'; b.async = true; + b.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'a.klaviyo.com/media/js/analytics/analytics.js'; + var a = document.getElementsByTagName('script')[0]; a.parentNode.insertBefore(b, a); } }; diff --git a/klaviyo.module b/klaviyo.module index 786dc74..fd69772 100644 --- a/klaviyo.module +++ b/klaviyo.module @@ -528,8 +528,7 @@ function klaviyo_page_alter(&$page) { global $user; $settings = variable_get('klaviyo_javascript', array()); - if (!empty($settings['key_public']) && _klaviyo_js_should_add_js($user)) { - + if (!empty($settings['public_api_key']) && _klaviyo_js_should_add_js($user)) { $page['content']['#attached']['js'][] = array( 'data' => drupal_get_path('module', 'klaviyo') . '/klaviyo.js', 'type'=>'file', @@ -537,18 +536,24 @@ function klaviyo_page_alter(&$page) { 'scope' => 'footer', ); + $identify = array(); + if (!empty($user->mail)) { + $identify = array( + 'identify' => array( + '$email' => $user->mail, + 'drupal.site_id' => klaviyo_api_get_instance()->getSiteId(), + ), + ); + } + $page['content']['#attached']['js'][] = array( 'data' => array( 'klaviyo' => array( - 'identify' => array( - '$email' => $user->mail, - ), - 'key_public' => $settings['key_public'] - ), + 'public_api_key' => $settings['public_api_key'] + ) + $identify, ), 'type' => 'setting', ); - } }