diff --git a/includes/klaviyo.admin.inc b/includes/klaviyo.admin.inc
index 7d02b7b..85fd004 100644
--- a/includes/klaviyo.admin.inc
+++ b/includes/klaviyo.admin.inc
@@ -52,7 +52,9 @@ function klaviyo_api_integration_settings() {
$form['klaviyo_javascript'] = array(
'#type' => 'fieldset',
'#title' => t('Javascript API'),
- '#description' => t('ADD SOME DESCRIPTION HERE TO EXPLAIN JS VS SERVER API.'),
+ '#description' => t('The JavaScript API adds automatic tracking of visited pages such as the "Active on site" metric. See the Klaviyo jasvascript API documentation for more information. Enter the public API key to enable the snippet.',
+ array('!url' => 'https://www.klaviyo.com/docs/getting-started')
+ ),
'#collapsible' => TRUE,
'#tree' => TRUE
);
@@ -60,14 +62,13 @@ function klaviyo_api_integration_settings() {
$form['klaviyo_javascript']['key_public'] = array(
'#type' => 'textfield',
'#title' => t('Public API key'),
- '#description' => t('The public API key is used in order to send events via the JavaScript integration. 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.',
+ '#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'],
'#size' => 60,
'#maxlength' => 128,
'#required' => FALSE,
- '#group' => 'klaviyo',
);
// @todo: Add ability for black list vs white list.
diff --git a/klaviyo.install b/klaviyo.install
index 0bf6604..75fa2a5 100644
--- a/klaviyo.install
+++ b/klaviyo.install
@@ -68,6 +68,7 @@ function klaviyo_schema() {
function klaviyo_uninstall() {
variable_del('klaviyo_api_key');
variable_del('klaviyo_drupal_site_id');
+ variable_del('klaviyo_javascript');
variable_del('klaviyo_entity_settings_user');
variable_del('klaviyo_person_attributes_user');
}
diff --git a/klaviyo.js b/klaviyo.js
new file mode 100644
index 0000000..22d86ed
--- /dev/null
+++ b/klaviyo.js
@@ -0,0 +1,29 @@
+/**
+ * @file
+ * Provides JavaScript for Klaviyo.
+ */
+
+(function ($) {
+
+Drupal.behaviors.klaviyo = {
+ attach: function (context) {
+ var identity = Drupal.settings.klaviyo.identify.$email
+ var _learnq = _learnq || [];
+
+ _learnq.push(['account', Drupal.settings.klaviyo.key_public]);
+
+ if (identity) {
+ _learnq.push(['identify', {
+ '$email' : identity
+ }]);
+ };
+
+ (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);
+ })();
+ }
+};
+
+})(jQuery);
diff --git a/klaviyo.module b/klaviyo.module
index f1ebda8..799ae92 100644
--- a/klaviyo.module
+++ b/klaviyo.module
@@ -530,30 +530,19 @@ function klaviyo_page_alter(&$page) {
$settings = variable_get('klaviyo_javascript', array());
if (!empty($settings['key_public']) && _klaviyo_js_should_add_js($user)) {
- if ($user->mail) {
- $identify = "_learnq.push(['identify', {" .
- "'\$email' : '" . $user->mail . "'" .
- "}]);";
- }
- else {
- $identify = '';
- }
-
- $script = "var _learnq = _learnq || [];" .
- "_learnq.push(['account', '" . $settings['key_public'] . "']);" .
- $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);" .
- "})();";
+ drupal_add_js(array(
+ 'klaviyo' => array(
+ 'identify' => array(
+ '$email' => $user->mail,
+ ),
+ 'key_public' => $settings['key_public']
+ )), 'setting');
- drupal_add_js($script, array(
+ drupal_add_js(drupal_get_path('module', 'klaviyo') . '/klaviyo.js', array(
'scope' => 'footer',
- 'type' => 'inline',
+ 'type' => 'file',
'weight' => 0
- )
- );
+ ));
}
}