diff --git a/includes/klaviyo.admin.inc b/includes/klaviyo.admin.inc index e860806..7d02b7b 100644 --- a/includes/klaviyo.admin.inc +++ b/includes/klaviyo.admin.inc @@ -48,14 +48,12 @@ function klaviyo_api_integration_settings() { ); } - // START Javascript settings - $settings = variable_get('klaviyo_javascript', ''); - + $js_settings = variable_get('klaviyo_javascript', array('key_public' => '', 'roles' => array())); $form['klaviyo_javascript'] = array( '#type' => 'fieldset', - '#title' => t('Klaviyo javascript settings'), + '#title' => t('Javascript API'), + '#description' => t('ADD SOME DESCRIPTION HERE TO EXPLAIN JS VS SERVER API.'), '#collapsible' => TRUE, - '#group' => 'klaviyo', '#tree' => TRUE ); @@ -65,27 +63,23 @@ function klaviyo_api_integration_settings() { '#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.', array('!url' => 'https://www.klaviyo.com/account#api-keys-tab') ), - '#default_value' => $settings['key_public'], + '#default_value' => $js_settings['key_public'], '#size' => 60, '#maxlength' => 128, '#required' => FALSE, '#group' => 'klaviyo', ); - $roles = user_roles(); - $role_options = array(); - foreach ($roles as $rid => $name) { - $role_options[$rid] = $name; - } + // @todo: Add ability for black list vs white list. $form['klaviyo_javascript']['roles'] = array( '#type' => 'checkboxes', '#title' => t('Remove script for specific roles'), - '#default_value' => $settings['roles'] ? $settings['roles'] : array(), - '#options' => $role_options, + '#default_value' => $js_settings['roles'], + '#options' => user_roles(), '#description' => t('Remove script only for the selected role(s). If none of the roles are selected, all roles will have the script. Otherwise, any roles selected here will NOT have the script.'), ); - // END Javascript settings + // @todo: Add ability to exclude on certain pages. return system_settings_form($form); } diff --git a/klaviyo.module b/klaviyo.module index c1d93c7..f1ebda8 100644 --- a/klaviyo.module +++ b/klaviyo.module @@ -523,16 +523,12 @@ function klaviyo_get_installation_instructions() { /** * Implementation of hook_page_alter(). - * - * @global object $user - * full user object for current user - * @param $page - * nested array of renderable elements that make up the page. */ function klaviyo_page_alter(&$page) { global $user; - $settings = variable_get('klaviyo_javascript', ''); - if (!empty($settings['key_public']) && _klaviyo_visibility_roles($user)) { + + $settings = variable_get('klaviyo_javascript', array()); + if (!empty($settings['key_public']) && _klaviyo_js_should_add_js($user)) { if ($user->mail) { $identify = "_learnq.push(['identify', {" . @@ -562,27 +558,23 @@ function klaviyo_page_alter(&$page) { } /** - * Based on visibility setting this function returns TRUE if Klaviyo code should - * be added for the current role and otherwise FALSE. + * Determine if the JS tracking snippet should be added to a page. * * @param object $account - * a full user object. + * (optional) The user account to check, if not given use currently logged in user. * @return boolean - * a decision on visibility, true/false + * Returns TRUE if the JS tracking snippt should be added to a page and FALSE + * otherwise. */ -function _klaviyo_visibility_roles($account) { - $enabled = TRUE; - $roles = variable_get('klaviyo_javascript', array())['roles']; - if (array_sum($roles) > 0) { - // One or more roles are selected for tracking. - foreach (array_keys($account->roles) as $rid) { - // Is the current user a member of one role selected in admin settings? - if (isset($roles[$rid]) && $rid == $roles[$rid]) { - // Current user is a member of a role that is selected in admin settings. - $enabled = FALSE; - break; - } - } +function _klaviyo_js_should_add_js($account = NULL) { + if (is_null($account)) { + global $user; + $account = $user; } - return $enabled; + + $js_settings = variable_get('klaviyo_javascript', array()); + $roles = !empty($js_settings['roles']) ? $js_settings['roles'] : array(); + $role_intersection = array_intersect_key($account->roles, array_flip($js_settings['roles'])); + + return empty($role_intersection); }