KWSGFDaddyAnalyticsAddon

The Gravity Forms Salesforce Add-On KWSGFDaddyAnalyticsAddon class.

Defined (1)

The class is defined in the following location(s).

/inc/daddy_analytics.addon.php  
  1. class KWSGFDaddyAnalyticsAddon extends KWSGFAddOn2_2 { 
  2. protected $_version; 
  3. protected $_min_gravityforms_version = "1.7"; 
  4. protected $_slug = "sf-loader-daddy-analytics"; 
  5. protected $_path = "gravity-forms-salesforce/inc/daddy_analytics.addon.php"; 
  6. protected $_full_path = __FILE__; 
  7. protected $_title = "Gravity Forms: Salesforce Daddy Analytics Add-On"; 
  8. protected $_short_title = "Salesforce: Daddy Analytics"; 
  9. protected $_service_name = "Daddy Analytics"; 
  10. protected $_custom_field_placeholder = "Daddy Analytics Site ID"; 
  11.  
  12. protected $_support_logging = false; 
  13. protected $_has_form_settings_page = false; 
  14.  
  15. var $token; 
  16. var $url; 
  17. var $site_id; 
  18.  
  19. function __construct() { 
  20.  
  21. $this->_version = KWS_GF_Salesforce::version; 
  22.  
  23. parent::__construct(); 
  24.  
  25. $this->token = $this->get_plugin_setting('daddy_analytics_token'); 
  26. $this->url = $this->get_plugin_setting('daddy_analytics_webtolead_url_id'); 
  27. $this->token_api_name = $this->get_plugin_setting('daddy_analytics_token_api_name'); 
  28. $this->url_api_name = $this->get_plugin_setting('daddy_analytics_webtolead_url_api_name'); 
  29. $this->site_id = $this->get_plugin_setting('daddy_analytics_site_id'); 
  30.  
  31. $this->initialize(); 
  32.  
  33. /** 
  34. * Add hooks 
  35. * @since 3.1.2 
  36. */ 
  37. function initialize() { 
  38.  
  39. add_filter('gf_salesforce_loader_fields', array(&$this, 'add_field_to_salesforce_loader')); 
  40. add_filter( 'gf_salesforce_lead_source_label', array(&$this, 'modify_lead_source_label')); 
  41.  
  42.  
  43. // Add fields to form 
  44. add_filter( 'gform_submit_button', array(&$this, 'add_fields_html'), 10, 2); 
  45.  
  46. // API Add-on 
  47. add_filter( 'gf_salesforce_create_data', array(&$this, 'filter_api_merge_vars'), 10, 5); 
  48.  
  49. // Web-to-Lead Add-on 
  50. add_filter( 'gf_salesforce_push_data', array(&$this, 'filter_web_to_lead_merge_vars'), 10, 3); 
  51.  
  52. // Add tracking script 
  53. add_action('gform_preview_footer', array(&$this, 'daddy_analytics_javascript')); 
  54. add_action('wp_footer', array(&$this, 'daddy_analytics_javascript')); 
  55.  
  56. /** 
  57. * Add the Daddy Analytics URL and token to the Web-to-Lead data 
  58. * @param array $merge_vars 
  59. * @param array $form GF Form 
  60. * @param array $entry GF Entry 
  61. * @return array Modified $merge_vars 
  62. */ 
  63. function filter_web_to_lead_merge_vars($merge_vars, $form, $entry) { 
  64.  
  65. do_action('kwsgfwebtoleadaddon_log_debug', 'DA::filter_web_to_lead_merge_vars() - Starting adding DA data to merge vars.'); 
  66.  
  67. // Get the $_POST data for the inserted fields 
  68. $submitted_token = rgpost(esc_attr($this->token), true); 
  69. $submitted_url = rgpost(esc_attr($this->url), true); 
  70.  
  71. // Add the data to be pushed. 
  72. $merge_vars[esc_attr($this->url)] = $submitted_url; 
  73. $merge_vars[esc_attr($this->token)] = $submitted_token; 
  74.  
  75. do_action('kwsgfwebtoleadaddon_log_debug', "DA::filter_api_merge_vars() - Added DA data to merge vars. Token: {$submitted_token} and Url: {$submitted_url}"); 
  76.  
  77. return $merge_vars; 
  78.  
  79. /** 
  80. * Add the Daddy Analytics URL and token to the Salesforce API data 
  81. * @param array $merge_vars 
  82. * @param array $form GF Form 
  83. * @param array $entry GF Entry 
  84. * @param array $feed Gravity Forms GFFeedAddon array 
  85. * @param SforcePartnerClient|SforceEnterpriseClient $api API object, as fetched from GFSalesforce::get_api() 
  86. * @return array Modified $merge_vars 
  87. */ 
  88. function filter_api_merge_vars($merge_vars, $form, $entry, $feed, $api ) { 
  89.  
  90. if(class_exists('GFSalesforce')) { 
  91. GFSalesforce::log_debug('DA::filter_api_merge_vars() - Starting adding DA data to merge vars.'); 
  92.  
  93. // Daddy Analytics only works on Leads. If the object type isn't a lead,  
  94. // return the original data. 
  95. if(isset($feed['meta']) && isset($feed['meta']['contact_object_name'])) { 
  96. if($feed['meta']['contact_object_name'] !== 'Lead') { 
  97. GFSalesforce::log_debug('DA::filter_api_merge_vars() - Not a Lead object.'); 
  98. return $merge_vars; 
  99.  
  100. // Get the $_POST data for the inserted fields 
  101. $submitted_token = rgpost(esc_attr($this->token), true); 
  102. $submitted_url = rgpost(esc_attr($this->url), true); 
  103.  
  104. // Use defaults if not set yet, somehow. 
  105. $url_api_name = empty($this->url_api_name) ? 'DaddyAnalytics__DA_Web_to_Lead_URL__c' : $this->url_api_name; 
  106. $token_api_name = empty($this->token_api_name) ? 'DaddyAnalytics__DA_Token__c' : $this->token_api_name; 
  107.  
  108. // Add the data to be pushed. 
  109. $merge_vars[esc_attr($url_api_name)] = $submitted_url; 
  110. $merge_vars[esc_attr($token_api_name)] = $submitted_token; 
  111.  
  112. if(class_exists('GFSalesforce')) { 
  113. if(empty($this->url_api_name)) { 
  114. GFSalesforce::log_error("DA::filter_api_merge_vars() - Daddy Analytics Web-to-Lead API Name is not set."); 
  115.  
  116. if(empty($this->token_api_name)) { 
  117. GFSalesforce::log_error("DA::filter_api_merge_vars() - Daddy Analytics Token API Name is not set."); 
  118.  
  119. GFSalesforce::log_debug("DA::filter_api_merge_vars() - Added DA data to merge vars. Token: {$submitted_token} and Url: {$submitted_url}"); 
  120.  
  121. return $merge_vars; 
  122.  
  123. /** 
  124. * Add the hidden fields to forms with Salesforce integrations. 
  125. * @param string $html Submit button input 
  126. * @param array $form GF form array 
  127. */ 
  128. function add_fields_html( $html, $form ) { 
  129.  
  130. // If the form's been modified by KWSAddon::add_feed_status_to_form() or GFSalesforce::add_feed_status_to_form() 
  131. // then it qualifies for DA integration 
  132. if(!empty($form['feed-gravity-forms-salesforce']) || !empty($form['feed-sf-web-to-lead'])) { 
  133. $html .= '<!-- Begin Daddy Analytics fields -->'; 
  134. $html .= '<input type="hidden" name="'.esc_attr($this->token).'" value="" />'; 
  135. $html .= '<input type="hidden" name="'.esc_attr($this->url).'" value="" />'; 
  136. $html .= '<!-- End Daddy Analytics fields -->'; 
  137.  
  138. return $html; 
  139.  
  140. /** 
  141. * Add the logo for the Feeds page 
  142. * @return string IMG HTML tag 
  143. */ 
  144. public function get_service_icon() { 
  145. return '<img src="'.plugins_url( 'assets/images/daddy_analytics/icon_50.png', KWS_GF_Salesforce::$file ).'" class="alignleft" style="margin:0 10px 10px 0" />'; 
  146.  
  147. public function get_service_favicon_path() { 
  148. return plugins_url( 'assets/images/daddy_analytics/icon_16.png', KWS_GF_Salesforce::$file ); 
  149.  
  150. /** 
  151. * @inheritDoc 
  152. */ 
  153. public function plugin_settings_fields() { 
  154.  
  155. $fields = array(); 
  156.  
  157. $fields[] = array( 
  158. "title" => sprintf(__("%s Configuration", 'gravity-forms-salesforce'), $this->get_service_name()),  
  159. "description" => $this->get_ad_text(),  
  160. "class" => 'clear',  
  161. "fields" => array( 
  162. array( 
  163. "name" => "daddy_analytics_token",  
  164. "label" => __("Token", "gravity-forms-salesforce"),  
  165. "type" => "text",  
  166. "class" => "medium code",  
  167. ),  
  168. array( 
  169. "name" => "daddy_analytics_webtolead_url_id",  
  170. "label" => __("Web to Lead URL ID", "gravity-forms-salesforce"),  
  171. "type" => "text",  
  172. "class" => "medium code",  
  173. ),  
  174. array( 
  175. "name" => "daddy_analytics_site_id",  
  176. "label" => __("Site ID", "gravity-forms-salesforce"),  
  177. "type" => "text",  
  178. "class" => "medium code",  
  179. ),  
  180. array( 
  181. "name" => "add_js",  
  182. "label" => sprintf(__("Add Javascript? %sDisable you want to add tracking code yourself.%s", "gravity-forms-salesforce"), '<span class="howto">', '</span>'),  
  183. "type" => "checkbox",  
  184. "dependency" => array(&$this, 'using_da'),  
  185. "choices" => array( 
  186. array( 
  187. "name" => 'yes',  
  188. "label" => 'Add tracking script to the site footer?',  
  189. "value" => 1,  
  190. 'default_value' => 1,  
  191. ),  
  192. ),  
  193. array( 
  194. 'name' => 'Save',  
  195. 'type' => 'save',  
  196. 'messages' => array( 
  197. 'success' => __('Settings updated successfully.', 'gravity-forms-salesforce'),  
  198. 'error' => __('Settings failed to update successfully.', 'gravity-forms-salesforce') 
  199. ); 
  200.  
  201. // If you need to define custom API names, you can do so here. 
  202. // Sometimes Salesforce adds prefixes. 
  203. if( apply_filters( 'gf_salesforce_custom_da_api_names', false ) ) { 
  204. $fields[] = array( 
  205. "title" => __("Salesforce API Name Configuration", 'gravity-forms-salesforce'),  
  206. "description" => wpautop(sprintf(__('Make sure the API names below match what is in Salesforce on your %sLead Fields%s page.', 'gravity-forms-salesforce'), '<a href="https://na11.salesforce.com/p/setup/layout/LayoutFieldList?setupid=LeadFields&type=Lead">', '</a>')).wpautop(sprintf('<img src="%s" width="658" height="133" style="max-width:100%%;" alt="Salesforce API Fields" />', plugins_url( 'assets/images/daddy_analytics/Salesforce_API_Fields.png', KWS_GF_Salesforce::$file ))),  
  207. "style" => 'padding-top: 0;',  
  208. 'fields' => array( 
  209. array( 
  210. "name" => "daddy_analytics_webtolead_url_api_name",  
  211. "label" => __("Web-to-Lead URL API Name", "gravity-forms-salesforce"),  
  212. "type" => "text",  
  213. "default_value" => 'DaddyAnalytics__DA_Web_to_Lead_URL__c',  
  214. "class" => "large code",  
  215. ),  
  216. array( 
  217. "name" => "daddy_analytics_token_api_name",  
  218. "label" => __("Daddy Analytics Token API Name", "gravity-forms-salesforce"),  
  219. "type" => "text",  
  220. "default_value" => 'DaddyAnalytics__DA_Token__c',  
  221. "class" => "large code",  
  222. ),  
  223. array( 
  224. 'name' => 'Save',  
  225. 'type' => 'save',  
  226. 'messages' => array( 
  227. 'success' => __('Settings updated successfully.', 'gravity-forms-salesforce'),  
  228. 'error' => __('Settings failed to update successfully.', 'gravity-forms-salesforce') 
  229. ); 
  230.  
  231. return $fields; 
  232.  
  233. /** 
  234. * @return string 
  235. */ 
  236. private function get_ad_term() { 
  237. global $plugin_page; 
  238.  
  239. if($plugin_page === 'gf_settings') { 
  240. $term = 'settings'; 
  241. }else{ 
  242. $term = 'form'; 
  243.  
  244. return $term; 
  245.  
  246. private function get_ad_link( $content, $medium, $url = 'http://daddyanalytics.com/', $term = '', $source = 'G_forms', $campaign = 'KWS_GF_Salesforce' ) { 
  247.  
  248. if( !$term ) { 
  249. $term = $this->get_ad_term(); 
  250.  
  251. $link = $url . '?utm_source=%s&utm_medium=%s&utm_campaign=%s&utm_term=%s&utm_content=%s'; 
  252.  
  253. return sprintf( $link, $source, $medium, $campaign, $term, $content ); 
  254.  
  255.  
  256. function get_ad_code( $type, $force = false, $id = null, $num = null) { 
  257.  
  258. if( defined( 'KWSGFSF_HIDE_ADS' ) && SFWP2L_HIDE_ADS == true ) { 
  259. return; // hide ads due to constant 
  260. }elseif( defined( 'KWSGFSF_HIDE_ADS' ) && SFWP2L_HIDE_ADS == false ) { 
  261. // show ads anyways 
  262. }else{ 
  263. if( $this->using_da() && empty($force) ) 
  264. return; // hide ads as they've signed up 
  265.  
  266. $assets_path = 'assets/images/daddy_analytics/'; 
  267.  
  268. $ads = array( 
  269.  
  270. 'banner-main' => array( 
  271. array( 
  272. 'url' => 'https://breadwinnerhq.com?utm_campaign=GF_BW_2&utm_source=G_forms&utm_medium=banner&utm_term=connect+salesforce+xero',  
  273. 'content' => $assets_path . 'Breadwinner-connect-salesforce-xero.png' 
  274. ),  
  275. array( 
  276. 'url' => 'https://breadwinnerhq.com?utm_campaign=GF_BW_2&utm_source=G_forms&utm_medium=banner&utm_term=create+invoices+in+xero',  
  277. 'content' => $assets_path . 'Breadwinner-create-invoices-in-xero.png' 
  278. ),  
  279. array( 
  280. 'url' => 'https://breadwinnerhq.com?utm_campaign=GF_BW_2&utm_source=G_forms&utm_medium=banner&utm_term=never+miss+again',  
  281. 'content' => $assets_path . 'Breadwinner-never-miss-again.png' 
  282. ),  
  283. array( 
  284. 'url' => 'https://daddydanalytics.com?utm_campaign=GF_DA_2&utm_source=G_forms&utm_medium=banner&utm_term=track+google+adwords',  
  285. 'content' => $assets_path . 'Track-Google-Adwords_banner.png' 
  286. ),  
  287. array( 
  288. 'url' => 'https://daddydanalytics.com?utm_campaign=GF_DA_2&utm_source=G_forms&utm_medium=banner&utm_term=track+lead+source',  
  289. 'content' => $assets_path . 'Track-Lead-Source_banner.png' 
  290. ),  
  291. ),  
  292. 'text' => array( 
  293. array( 
  294. 'id' => 'da1_7',  
  295. 'content' => sprintf( __( 'Daddy Analytics allows you to track your leads from their original source, such as AdWords, Google Organic, Social Media, or other blogs. With that information you can get your true marketing ROI, as each Opportunity is attributed to the marketing activity that brought in the Lead. %sWatch a video of Daddy Analytics%s', 'gravity-forms-salesforce' ), '<p class="submit"><a class="button button-secondary" href="%link1%" target="_blank">', '</a></p>' ) 
  296. ),  
  297. array( 
  298. 'id' => 'da1_8',  
  299. 'cta' => 'Sign up Now',  
  300. 'content' => sprintf( __( 'Daddy Analytics allows you to track your leads from their original source, such as AdWords, Google Organic, Social Media, or other blogs. With that information you can get your true marketing ROI, as each Opportunity is attributed to the marketing activity that brought in the Lead. %sSign up for a free trial of Daddy Analytics%s', 'gravity-forms-salesforce' ), '<p class="submit"><a class="button button-secondary" href="%link2%" target="_blank">', '</a></p>' ) 
  301. ),  
  302. ),  
  303.  
  304. ); 
  305.  
  306.  
  307. $num = mt_rand( 1, count( $ads[ $type ] ) ) - 1; 
  308.  
  309. return $ads[ $type ][ $num ]; 
  310.  
  311. /** 
  312. * Get a banner ad for Daddy Analytics 
  313. * @param boolean $force Force showing, even if DA is configured? 
  314. * @return string HTML of ad 
  315. */ 
  316. function get_ad_banner($force = false) { 
  317.  
  318. $banner = ''; 
  319.  
  320. if( $ad = $this->get_ad_code('banner-main', $force) ) { 
  321.  
  322. $link = $ad['url']; 
  323.  
  324. // Margin-top is to make the transparency look better 
  325. $banner = '<div><div class="hr-divider"></div>'; 
  326. $banner .= '<a href="'.$link.'" target="_blank"><img src="'.plugins_url( $ad['content'], KWS_GF_Salesforce::$file ).'" width="586" height="147" style="'; 
  327. $banner .= 'width:auto; height:auto; max-width:100%; max-height:180px;'; // Scale down when window is smaller; don't get too big when wide window. 
  328. $banner .= '" /></a></div>'; 
  329.  
  330. return $banner; 
  331.  
  332. /** 
  333. * Get a text ad for Daddy Analytics 
  334. * @param boolean $force Force showing, even if DA is configured? 
  335. * @return string HTML of ad 
  336. */ 
  337. function get_ad_text($force = false) { 
  338.  
  339. if( $ad = $this->get_ad_code('text', $force) ) { 
  340.  
  341. $link1 = $this->get_ad_link( $ad['id'], 'text', 'https://daddydanalytics.com' ); 
  342. $link2 = $this->get_ad_link( $ad['id'], 'text', 'https://daddyanalytics.com' ); 
  343.  
  344. $content = str_replace( array('%link1%', '%link2%'), array($link1, $link2), $ad['content'] ); 
  345.  
  346. }else{ 
  347. $content = '<div class="updated inline widefat">'.wpautop(sprintf(__('Thank you for using %sDaddy Analytics%s!', 'gravity-forms-salesforce'), '<a href="http://daddyanalytics.com/" target="_blank">', '</a>' )).'</div>'; 
  348.  
  349. return wpautop( $content ); 
  350.  
  351. /** 
  352. * Add DA helper text to the Lead Source label, if DA is enabled. 
  353. * @param string $label Existing label 
  354. * @return string Modified label, if using DA. Unmodified if not. 
  355. */ 
  356. function modify_lead_source_label($label = '') { 
  357.  
  358. if($this->using_da()) { 
  359. $label .= '<span class="howto">'.sprintf(__('%sLeave the Lead Source field blank%s for Daddy Analytics. Daddy Analytics will populate the Lead Source field with the web source of the Lead (such as Organic - Google, Paid - Bing, or Google Adwords).', 'gravity-forms-salesforce'), '<strong>', '</strong>').'</div>'; 
  360.  
  361. return $label; 
  362.  
  363.  
  364. function add_field_to_salesforce_loader($fields) { 
  365.  
  366. if($this->using_da()) { return $fields; } 
  367.  
  368. $banner = $this->get_ad_banner(); 
  369.  
  370. $content = $this->get_ad_text(); 
  371.  
  372. $da_field = array( 
  373. 'type' => 'html',  
  374. 'id' => 'daddyanalytics_ad',  
  375. 'title' => __('Daddy Analytics', 'gravity-forms-salesforce'),  
  376. 'value' => $banner.$content,  
  377. ); 
  378.  
  379. // Insert the field before the "save" button field 
  380. $fields[] = $da_field; 
  381.  
  382. return $fields; 
  383.  
  384. function using_da() { 
  385. return !empty($this->token) && !empty($this->url) && !empty($this->site_id); 
  386.  
  387. protected function valid_api_message() { 
  388. return '<span class="gf_keystatus_valid_text">'.sprintf(__("%s Active: Your Daddy Analytics configuration is valid.", 'gravity-forms-salesforce'), '<i class="fa fa-check gf_keystatus_valid"></i>').'</span>'; 
  389.  
  390. protected function invalid_api_message() { 
  391.  
  392. $tofind = sprintf(__('Please login to your Daddy Analytics account to obtain your account information.', 'gravity-forms-salesforce')); 
  393. if(empty($this->site_id)) { 
  394. $message = __("Please complete all fields for your Daddy Analytics account.", 'gravity-forms-salesforce'); 
  395. } else { 
  396. $message = '<span class="gf_keystatus_invalid_text">'.sprintf(__("%s Invalid field values - Please confirm your settings.", 'gravity-forms-salesforce'), '<i class="fa fa-times gf_keystatus_invalid"></i>').'</span>'; 
  397.  
  398. return '<h4>'.$message.'</h4>'.$tofind; 
  399.  
  400. /** 
  401. * Is the API valid? 
  402. * Stores validity in `$_service_api_valid` and re-checks if the form has been submitted. 
  403. * @return boolean True: Org ID is valid; False: Org ID is invalid. 
  404. */ 
  405. public function is_valid_api() { 
  406.  
  407. // If the form hasn't just been saved, then use the stored value. 
  408. // Otherwise, we should check again if the settings are valid. 
  409. if(!is_null($this->_service_api_valid) && empty($_POST['gform-settings-save'])) { 
  410. return $this->_service_api_valid; 
  411.  
  412. //@TODO: Find way to validate Daddy Analytics config options 
  413. //check for non-empty values in all 3 fields 
  414.  
  415. $this->_service_api_valid = $this->using_da(); 
  416.  
  417. return $this->_service_api_valid; 
  418.  
  419.  
  420. /** 
  421. * Generate the javascript code for DA 
  422. * @param boolean $echo True: echo the javascript; False: just return the JS 
  423. * @return $output HTML output 
  424. */ 
  425. public function daddy_analytics_javascript($echo = true) { 
  426.  
  427. // If DA isn't configured, don't output anything. 
  428. if(!$this->using_da()) { return; } 
  429.  
  430. $protocol = is_ssl() ? 'https://' : 'http://'; 
  431. $site_id = esc_attr($this->site_id); 
  432. $token = esc_attr($this->token); 
  433. $url = esc_attr($this->url); 
  434.  
  435. $output = <<<EOD 
  436.  
  437. <!-- Begin Daddy Analytics code provided by Gravity Forms Salesforce Plugin --> 
  438. <script src="{$protocol}cdn.daddyanalytics.com/w2/daddy.js" type="text/javascript"></script> 
  439. <script type="text/javascript"> 
  440. var da_data = daddy_init('{ "da_token" : "{$token}", "da_url" : "{$url}" }'); 
  441. var clicky_custom = {session: {DaddyAnalytics: da_data}}; 
  442. </script> 
  443. <script src="{$protocol}hello.staticstuff.net/w/__stats.js" type="text/javascript"></script> 
  444. <script type="text/javascript">try{ clicky.init( "{$site_id}" ); }catch(e) {}</script> 
  445. <!-- End Daddy Analytics code provided by Gravity Forms Salesforce Plugin --> 
  446.  
  447. EOD; 
  448.  
  449. echo $output; 
  450. return $output;