KJ_BetterVisitorLog - Version 1.0.0

Version Notes

Download this release

Release Info

Developer Kalen Jordan
Extension KJ_BetterVisitorLog
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

KJ-BETTER-VISITOR-LOG-README.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ Better Visitor Log
2
+ ==================
3
+
4
+ A client-side replacement for Magento visitor log
5
+
6
+ Go to https://magemail.co/better-visitor-log-for-magento.html
app/code/community/KJ/BetterVisitorLog/Helper/Data.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class KJ_BetterVisitorLog_Helper_Data extends Mage_Core_Helper_Data
4
+ {
5
+ public function getAjaxBaseUrl()
6
+ {
7
+ $base = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, $this->_getRequest()->isSecure());
8
+ $base .= "kjbvl";
9
+
10
+ return $base;
11
+ }
12
+
13
+ public function currentProductId()
14
+ {
15
+ $product = Mage::registry('current_product');
16
+ if (!($product instanceof Mage_Catalog_Model_Product)) {
17
+ return null;
18
+ }
19
+
20
+ return $product->getId();
21
+ }
22
+
23
+ public function isLocalLoggingEnabled()
24
+ {
25
+ return Mage::getStoreConfig('system/kj_bettervisitorlog/enable_local_logging', Mage::app()->getStore());
26
+ }
27
+
28
+ public function isMixpanelLoggingEnabled()
29
+ {
30
+ return Mage::getStoreConfig('system/kj_bettervisitorlog/enable_mixpanel', Mage::app()->getStore());
31
+ }
32
+
33
+ public function isConsoleLoggingEnabled()
34
+ {
35
+ return Mage::getStoreConfig('system/kj_bettervisitorlog/enable_console_logging', Mage::app()->getStore());
36
+ }
37
+
38
+ public function getMixpanelToken()
39
+ {
40
+ return Mage::getStoreConfig('system/kj_bettervisitorlog/mixpanel_token', Mage::app()->getStore());
41
+ }
42
+
43
+ public function getLoggedInCustomerEmail()
44
+ {
45
+ if (! Mage::getSingleton('customer/session')->isLoggedIn()) {
46
+ return null;
47
+ }
48
+
49
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
50
+ if (! $customer || ! ($customer instanceof Mage_Customer_Model_Customer)) {
51
+ return null;
52
+ }
53
+
54
+ return $customer->getData('email');
55
+ }
56
+ }
app/code/community/KJ/BetterVisitorLog/Model/Log.php ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @method getCreatedAt()
5
+ * @method KJ_BetterVisitorLog_Model_Log setCreatedAt($value)
6
+ * @method getEmail()
7
+ * @method KJ_BetterVisitorLog_Model_Log setEmail($value)
8
+ * @method getIpAddress()
9
+ * @method KJ_BetterVisitorLog_Model_Log setIpAddress($value)
10
+ * @method getXForwardedFor()
11
+ * @method KJ_BetterVisitorLog_Model_Log setXForwardedFor($value)
12
+ * @method getProductId()
13
+ * @method KJ_BetterVisitorLog_Model_Log setProductId($value)
14
+ */
15
+ class KJ_BetterVisitorLog_Model_Log extends Mage_Core_Model_Abstract
16
+ {
17
+ public function _construct()
18
+ {
19
+ parent::_construct();
20
+ $this->_init('kj_bettervisitorlog/log');
21
+ }
22
+ }
app/code/community/KJ/BetterVisitorLog/Model/Mysql4/Log.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class KJ_BetterVisitorLog_Model_Mysql4_Log extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ protected function _construct()
6
+ {
7
+ $this->_init('kj_bettervisitorlog/log', 'log_id');
8
+ }
9
+ }
app/code/community/KJ/BetterVisitorLog/Model/Mysql4/Setup.php ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ <?php
2
+
3
+ class KJ_BetterVisitorLog_Model_Mysql4_Setup extends Mage_Core_Model_Resource_Setup
4
+ {
5
+ }
app/code/community/KJ/BetterVisitorLog/controllers/LogController.php ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class KJ_BetterVisitorLog_LogController extends Mage_Core_Controller_Front_Action
4
+ {
5
+ public function viewAction()
6
+ {
7
+ try {
8
+ $this->_viewAction();
9
+ } catch (Exception $e) {
10
+ $message = "Exception";
11
+ if (Mage::getIsDeveloperMode()) {
12
+ $message .= ": " . $e->getMessage();
13
+ }
14
+
15
+ Mage::logException($e);
16
+ $this->_jsonResponse(array(
17
+ "success" => false,
18
+ "developer_mode" => Mage::getIsDeveloperMode(),
19
+ "message" => $message,
20
+ ));
21
+ }
22
+ }
23
+
24
+ protected function _viewAction()
25
+ {
26
+ if (! $this->_validateView()) {
27
+ return $this;
28
+ }
29
+
30
+ $productId = ($this->getRequest()->getParam('product_id') > 0) ? $this->getRequest()->getParam('product_id') : null;
31
+
32
+ $log = Mage::getModel('kj_bettervisitorlog/log');
33
+ $log->setEmail($this->getRequest()->getParam('email'))
34
+ ->setIpAddress(Mage::helper('core/http')->getRemoteAddr())
35
+ ->setXForwardedFor($this->getRequest()->getServer('HTTP_X_FORWARDED_FOR'))
36
+ ->setProductId($productId)
37
+ ->save();
38
+
39
+ $response = array(
40
+ "success" => true,
41
+ "developer_mode" => Mage::getIsDeveloperMode(),
42
+ );
43
+
44
+ if (Mage::getIsDeveloperMode()) {
45
+ $response['log_id'] = $log->getId();
46
+ }
47
+
48
+ $this->_jsonResponse($response);
49
+ return $this;
50
+ }
51
+
52
+ protected function _validateView()
53
+ {
54
+ $email = $this->getRequest()->getParam('email');
55
+ if (! $email) {
56
+ $this->_jsonError("Email is missing");
57
+ return false;
58
+ }
59
+
60
+ if (! Zend_Validate::is($email, 'EmailAddress')) {
61
+ $this->_jsonError("Email doesn't look valid");
62
+ return false;
63
+ }
64
+
65
+ return true;
66
+ }
67
+
68
+ protected function _jsonError($message)
69
+ {
70
+ $this->_jsonResponse(array(
71
+ "success" => false,
72
+ "message" => $message,
73
+ ));
74
+ }
75
+
76
+ protected function _jsonResponse($data)
77
+ {
78
+ $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
79
+ $this->getResponse()->setBody(json_encode($data));
80
+
81
+ return $this;
82
+ }
83
+ }
app/code/community/KJ/BetterVisitorLog/etc/adminhtml.xml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <menu>
4
+ <customer module="kj_bettervisitorlog" translate="title'">
5
+ <children>
6
+ <kj_bettervisitorlog>
7
+ <title>Better Visitor Log</title>
8
+ <sort_order>999</sort_order>
9
+ <action>adminhtml/system_config/edit/section/system</action>
10
+ </kj_bettervisitorlog>
11
+ </children>
12
+ </customer>
13
+ </menu>
14
+ </config>
app/code/community/KJ/BetterVisitorLog/etc/config.xml ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <KJ_BetterVisitorLog>
5
+ <version>1.0.0</version>
6
+ </KJ_BetterVisitorLog>
7
+ </modules>
8
+
9
+ <global>
10
+ <helpers>
11
+ <kj_bettervisitorlog>
12
+ <class>KJ_BetterVisitorLog_Helper</class>
13
+ </kj_bettervisitorlog>
14
+ </helpers>
15
+ <models>
16
+ <kj_bettervisitorlog>
17
+ <class>KJ_BetterVisitorLog_Model</class>
18
+ <resourceModel>kj_bettervisitorlog_mysql4</resourceModel>
19
+ </kj_bettervisitorlog>
20
+
21
+ <kj_bettervisitorlog_mysql4>
22
+ <class>KJ_BetterVisitorLog_Model_Mysql4</class>
23
+ <entities>
24
+ <log>
25
+ <table>kj_bettervisitorlog_log</table>
26
+ </log>
27
+ </entities>
28
+ </kj_bettervisitorlog_mysql4>
29
+ </models>
30
+
31
+ <resources>
32
+ <kj_bettervisitorlog_setup>
33
+ <setup>
34
+ <module>KJ_BetterVisitorLog</module>
35
+ <class>KJ_BetterVisitorLog_Model_Mysql4_Setup</class>
36
+ </setup>
37
+ <connection>
38
+ <use>core_setup</use>
39
+ </connection>
40
+ </kj_bettervisitorlog_setup>
41
+ <kj_bettervisitorlog_write>
42
+ <connection>
43
+ <use>core_write</use>
44
+ </connection>
45
+ </kj_bettervisitorlog_write>
46
+ <kj_bettervisitorlog_read>
47
+ <connection>
48
+ <use>core_read</use>
49
+ </connection>
50
+ </kj_bettervisitorlog_read>
51
+ </resources>
52
+
53
+ <blocks>
54
+ <kj_bettervisitorlog>
55
+ <class>KJ_BetterVisitorLog_Block</class>
56
+ </kj_bettervisitorlog>
57
+ </blocks>
58
+ </global>
59
+
60
+ <frontend>
61
+ <layout>
62
+ <updates>
63
+ <kj_bettervisitorlog>
64
+ <file>kj_bettervisitorlog.xml</file>
65
+ </kj_bettervisitorlog>
66
+ </updates>
67
+ </layout>
68
+ <routers>
69
+ <kj_bettervisitorlog>
70
+ <use>standard</use>
71
+ <args>
72
+ <module>KJ_BetterVisitorLog</module>
73
+ <frontName>kjbvl</frontName>
74
+ </args>
75
+ </kj_bettervisitorlog>
76
+ </routers>
77
+ </frontend>
78
+
79
+ <default>
80
+ <system>
81
+ <kj_bettervisitorlog>
82
+ <enable_console_logging>1</enable_console_logging>
83
+ </kj_bettervisitorlog>
84
+ </system>
85
+ </default>
86
+ </config>
app/code/community/KJ/BetterVisitorLog/etc/system.xml ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <system>
5
+ <groups>
6
+ <kj_bettervisitorlog>
7
+ <label>Better Visitor Log</label>
8
+ <show_in_default>1</show_in_default>
9
+ <show_in_website>1</show_in_website>
10
+ <show_in_store>1</show_in_store>
11
+ <sort_order>9999</sort_order>
12
+ <expanded>1</expanded>
13
+ <comment>
14
+ <![CDATA[
15
+ <div style="margin-top: -16px; background-color: rgb(231, 239, 239);margin-left: -20px;padding: 9px 3px; padding-right: 11px; margin-right: -20px;">
16
+ <a target="_blank" href="https://magemail.co/?utm_source=magento&amp;utm_medium=banner&amp;utm_campaign=better_visitor_logging">
17
+ <img src="https://magemail.co/app/skin/images/logo.png" style="
18
+ width: 73px;
19
+ float: left;
20
+ margin-top: -15px;
21
+ margin-left: -11px;
22
+ padding-right: 7px;
23
+ float: right;
24
+ padding-right: 3px;
25
+ padding-top: 14px;
26
+ ">
27
+ </a>
28
+ <a target="_blank" href="https://magemail.co/?utm_source=magento&amp;utm_medium=banner&amp;utm_campaign=better_visitor_logging"
29
+ style="color: #333;text-decoration: none;">
30
+ Better Visitor Logging is brought to you by <i>MageMail - the triggered email app for Magento</i>.
31
+ </a>
32
+ </div>
33
+ ]]>
34
+ </comment>
35
+ <fields>
36
+ <enable_local_logging translate="label">
37
+ <label>Log to Database</label>
38
+ <frontend_type>select</frontend_type>
39
+ <source_model>adminhtml/system_config_source_yesno</source_model>
40
+ <sort_order>100</sort_order>
41
+ <show_in_default>1</show_in_default>
42
+ <show_in_website>1</show_in_website>
43
+ <show_in_store>1</show_in_store>
44
+ <comment>
45
+ Log each visit to the store. This will store a lot
46
+ of data (technically a metric butt-tonne) if you have
47
+ a high-traffic site.
48
+ </comment>
49
+ </enable_local_logging>
50
+ <enable_mixpanel translate="label">
51
+ <label>Log to Mixpanel</label>
52
+ <frontend_type>select</frontend_type>
53
+ <source_model>adminhtml/system_config_source_yesno</source_model>
54
+ <sort_order>200</sort_order>
55
+ <show_in_default>1</show_in_default>
56
+ <show_in_website>1</show_in_website>
57
+ <show_in_store>1</show_in_store>
58
+ <comment>
59
+ Log visits to Mixpanel. You'll need to create an account
60
+ at mixpanel.com in order to use this.
61
+ </comment>
62
+ </enable_mixpanel>
63
+ <mixpanel_token translate="label">
64
+ <label>Mixpanel Token</label>
65
+ <sort_order>210</sort_order>
66
+ <show_in_default>1</show_in_default>
67
+ <show_in_website>1</show_in_website>
68
+ <show_in_store>1</show_in_store>
69
+ <comment>
70
+ You can get this from your Mixpanel account under
71
+ Account > Projects > Token.
72
+ </comment>
73
+ </mixpanel_token>
74
+ <enable_console_logging translate="label">
75
+ <label>Log to Javascript Console</label>
76
+ <frontend_type>select</frontend_type>
77
+ <source_model>adminhtml/system_config_source_yesno</source_model>
78
+ <sort_order>300</sort_order>
79
+ <show_in_default>1</show_in_default>
80
+ <show_in_website>1</show_in_website>
81
+ <show_in_store>1</show_in_store>
82
+ <comment>
83
+ This will log various things to the javascript console
84
+ like when an event is logged locally or to mixpanel. This
85
+ is useful when first installing / testing the extension.
86
+ </comment>
87
+ </enable_console_logging>
88
+ </fields>
89
+ </kj_bettervisitorlog>
90
+ </groups>
91
+ </system>
92
+ </sections>
93
+ </config>
app/code/community/KJ/BetterVisitorLog/etc/version.txt ADDED
@@ -0,0 +1 @@
 
1
+ 4926e2d99094ff2255daa550813e35355510c547
app/code/community/KJ/BetterVisitorLog/sql/kj_bettervisitorlog_setup/install-1.0.0.php ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /* @var $this Mage_Core_Model_Resource_Setup */
3
+ $this->startSetup();
4
+ $table = $this->getConnection()->newTable($this->getTable('kj_bettervisitorlog/log'));
5
+
6
+ $table->addColumn(
7
+ 'log_id',
8
+ Varien_Db_Ddl_Table::TYPE_INTEGER,
9
+ null,
10
+ array(
11
+ 'identity' => true,
12
+ 'primary' => true,
13
+ 'unsigned' => true,
14
+ 'nullable' => false,
15
+ )
16
+ );
17
+
18
+ $table->addColumn(
19
+ 'email',
20
+ Varien_Db_Ddl_Table::TYPE_VARCHAR,
21
+ 254
22
+ );
23
+
24
+ $table->addColumn(
25
+ 'product_id',
26
+ Varien_Db_Ddl_Table::TYPE_INTEGER,
27
+ null
28
+ );
29
+
30
+ $table->addColumn(
31
+ 'ip_address',
32
+ Varien_Db_Ddl_Table::TYPE_VARCHAR,
33
+ 46
34
+ );
35
+
36
+ $table->addColumn(
37
+ 'x_forwarded_for',
38
+ Varien_Db_Ddl_Table::TYPE_VARCHAR,
39
+ 46
40
+ );
41
+
42
+ $table->addColumn(
43
+ 'created_at',
44
+ Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
45
+ null,
46
+ array(
47
+ 'nullable' => false,
48
+ 'default' => Varien_Db_Ddl_Table::TIMESTAMP_INIT,
49
+ )
50
+ );
51
+
52
+ try {
53
+ $table->addIndex('IDX_KJ_BETTERVISITORLOG_EMAIL', array('email'));
54
+ $table->addIndex('IDX_KJ_BETTERVISITORLOG_PRODUCT_ID', array('product_id'));
55
+ $table->addIndex('IDX_KJ_BETTERVISITORLOG_CREATED_AT', array('created_at'));
56
+ $this->getConnection()->createTable($table);
57
+ } catch (Exception $e) {
58
+ Mage::logException($e);
59
+ }
60
+
61
+ $this->endSetup();
app/design/frontend/base/default/layout/kj_bettervisitorlog.xml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <layout version="1.6.1.0">
4
+ <default>
5
+ <block type="core/template" name="kj_bettervisitorlog_footer" template="kj_bettervisitorlog/footer.phtml"/>
6
+
7
+ <reference name="before_body_end">
8
+ <!-- ifconfig="kj_magemail/settings/enable_javascript" -->
9
+ <action method="append">
10
+ <name>kj_bettervisitorlog_footer</name>
11
+ </action>
12
+ </reference>
13
+ </default>
14
+ </layout>
app/design/frontend/base/default/template/kj_bettervisitorlog/footer.phtml ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php $productId = Mage::helper('kj_bettervisitorlog')->currentProductId(); ?>
2
+
3
+ <?php if (Mage::helper('kj_bettervisitorlog')->isMixpanelLoggingEnabled() && Mage::helper('kj_bettervisitorlog')->getMixpanelToken()): ?>
4
+ <!-- start Mixpanel -->
5
+ <script type="text/javascript">(function(f,b){if(!b.__SV){var a,e,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
6
+ for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=f.createElement("script");a.type="text/javascript";a.async=!0;a.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";e=f.getElementsByTagName("script")[0];e.parentNode.insertBefore(a,e)}})(document,window.mixpanel||[]);
7
+ mixpanel.init("<?php echo Mage::helper('kj_bettervisitorlog')->getMixpanelToken(); ?>");</script><!-- end Mixpanel -->
8
+ <?php endif; ?>
9
+
10
+ <!--[if gte IE 9]><!-->
11
+ <script>
12
+ KJ_BetterVisitorLog = {
13
+ init: function() {
14
+ this.observeEmailBlur();
15
+ this.logVisitor();
16
+ },
17
+
18
+ isLocalLoggingEnabled: function() {
19
+ return <?php echo Mage::helper('kj_bettervisitorlog')->isLocalLoggingEnabled() ? "true" : "false"; ?>;
20
+ },
21
+
22
+ isMixpanelLoggingEnabled: function() {
23
+ return <?php echo Mage::helper('kj_bettervisitorlog')->isMixpanelLoggingEnabled() ? "true" : "false"; ?>;
24
+ },
25
+
26
+ getEmailFromCookie: function() {
27
+ return Mage.Cookies.get('kjbvl_email');
28
+ },
29
+
30
+ saveEmailToCookie: function(email) {
31
+ var expires = new Date();
32
+ this.consoleLog("Saving email to cookie: " + email);
33
+
34
+ expires.setYear(expires.getFullYear() + 1);
35
+ return Mage.Cookies.set('kjbvl_email', email, expires);
36
+ },
37
+
38
+ consoleLog: function(message) {
39
+ var enabled = <?php echo Mage::helper('kj_bettervisitorlog')->isConsoleLoggingEnabled() ? "true" : "false"; ?>;
40
+ if (! enabled) {
41
+ return;
42
+ }
43
+
44
+ if (typeof(console) != 'undefined') {
45
+ console.log("[Better Visitor Log] " + message);
46
+ }
47
+ },
48
+
49
+ observeEmailBlur: function() {
50
+ var self = this;
51
+
52
+ Prototype.Selector.select('.validate-email, #login-email').each(function(element) {
53
+ element.stopObserving('blur');
54
+ Event.observe(element, 'blur', function(el) {
55
+ self._captureEmail(el);
56
+ });
57
+ });
58
+ },
59
+
60
+ _captureEmail: function(element) {
61
+ var email = (typeof element == 'string') ? element : element.target.value;
62
+ if (email) {
63
+ if (this.validateEmail(email)) {
64
+ this.consoleLog("Email entered and saved to cookie: " + email);
65
+ this.saveEmailToCookie(email);
66
+ } else {
67
+ this.consoleLog("Email doesn't look valid: " + email);
68
+ }
69
+ }
70
+ },
71
+
72
+ /**
73
+ * Going with a cheapy email validation for now. Found a nifty one but it
74
+ * didn't seem to properly support gmail style + emails:
75
+ *
76
+ * http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
77
+ */
78
+ validateEmail: function(email) {
79
+ return (email.indexOf('@') != -1 && email.indexOf('.') != -1);
80
+ },
81
+
82
+ logVisitor: function() {
83
+ if (! this.isLocalLoggingEnabled() && ! this.isMixpanelLoggingEnabled()) {
84
+ this.consoleLog("No logging is enabled");
85
+ return;
86
+ }
87
+
88
+ var email = this.detectEmail();
89
+ if (email) {
90
+ this.saveEmailToCookie(email);
91
+ } else {
92
+ email = this.getEmailFromCookie();
93
+ if (email) {
94
+ this.consoleLog("Using email from cookie: " + this.getEmailFromCookie());
95
+ }
96
+ }
97
+
98
+ if (! email) {
99
+ this.consoleLog("No email has been detected");
100
+ return;
101
+ }
102
+
103
+ if (this.isLocalLoggingEnabled()) {
104
+ this.logLocally();
105
+ }
106
+
107
+ if (this.isMixpanelLoggingEnabled()) {
108
+ this.logToMixpanel();
109
+ }
110
+ },
111
+
112
+ logLocally: function() {
113
+ var self = this;
114
+
115
+ var email = this.getEmailFromCookie();
116
+ var productId = <?php echo ($productId === null) ? "null" : $productId; ?>;
117
+ var url = '<?php echo Mage::helper('kj_bettervisitorlog')->getAjaxBaseUrl(); ?>/log/view/';
118
+
119
+ this.consoleLog("Logging locally");
120
+ this.consoleLog("Sending ajax request for email: " + email);
121
+
122
+ new Ajax.Request(url, {
123
+ method: 'post',
124
+ parameters: {
125
+ email: email,
126
+ product_id: productId
127
+ },
128
+
129
+ onCreate: self.fixAjaxRequestHeader,
130
+ onSuccess: function(transport) {
131
+ var data = self._transportToJson(transport);
132
+ if (data.success) {
133
+ self.consoleLog('Local logging successful');
134
+ } else {
135
+ self.consoleLog('There was a problem logging locally. Check the ajax response for more detail.');
136
+ }
137
+ },
138
+
139
+ onFailure: function() {
140
+ self.consoleLog('There was a problem logging locally. Check the ajax response for more detail.');
141
+ }
142
+ });
143
+ },
144
+
145
+ /**
146
+ * The prototype ajax request sometimes does weird things without this.
147
+ */
148
+ fixAjaxRequestHeader: function(response)
149
+ {
150
+ var t = response.transport;
151
+ t.setRequestHeader = t.setRequestHeader.wrap(function(original, k, v) {
152
+ if (/^(accept|accept-language|content-language)$/i.test(k))
153
+ return original(k, v);
154
+ if (/^content-type$/i.test(k) &&
155
+ /^(application\/x-www-form-urlencoded|multipart\/form-data|text\/plain)(;.+)?$/i.test(v))
156
+ return original(k, v);
157
+ return this;
158
+ });
159
+ },
160
+
161
+ /**
162
+ * Sometimes the data object returned from the prototype ajax request is
163
+ * a little cray. Prototype - gotta love it.
164
+ */
165
+ _transportToJson: function(transport)
166
+ {
167
+ if (transport.responseJSON) {
168
+ return transport.responseJSON;
169
+ }
170
+
171
+ if (transport.responseText) {
172
+ data = JSON.parse(transport.responseText);
173
+ return data;
174
+ }
175
+
176
+ return {};
177
+ },
178
+
179
+ logToMixpanel: function() {
180
+ var email = this.getEmailFromCookie();
181
+ this.consoleLog("Logging to mixpanel");
182
+
183
+ mixpanel.identify(email);
184
+ mixpanel.people.set({
185
+ "$email": email
186
+ });
187
+
188
+ mixpanel.track('page viewed', {
189
+ "page name" : document.title,
190
+ "product_id": <?php echo ($productId === null) ? "null" : $productId; ?>,
191
+ "url" : window.location.pathname
192
+ });
193
+ },
194
+
195
+ getQueryStringParameter: function(name) {
196
+ name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
197
+ var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
198
+ results = regex.exec(location.search);
199
+
200
+ return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
201
+ },
202
+
203
+ detectEmail: function() {
204
+ if (this.getQueryStringParameter('email')) {
205
+ return this.getQueryStringParameter('email');
206
+ }
207
+
208
+ if (this.getQueryStringParameter('mc_eid')) {
209
+ return this.getQueryStringParameter('mc_eid');
210
+ }
211
+
212
+ if (this.getQueryStringParameter('mm_recipient')) {
213
+ return this.getQueryStringParameter('mm_recipient');
214
+ }
215
+
216
+ var loggedInCustomerEmail = '<?php echo Mage::helper('kj_bettervisitorlog')->getLoggedInCustomerEmail(); ?>';
217
+ if (loggedInCustomerEmail) {
218
+ return loggedInCustomerEmail;
219
+ }
220
+
221
+ return null;
222
+ }
223
+ };
224
+
225
+ KJ_BetterVisitorLog.init();
226
+ </script>
227
+ <!--[[endif]><!-->
app/etc/modules/KJ_BetterVisitorLog.xml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <KJ_BetterVisitorLog>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends></depends>
8
+ </KJ_BetterVisitorLog>
9
+ </modules>
10
+ </config>
package.xml ADDED
@@ -0,0 +1,2 @@
 
 
1
+ <?xml version="1.0"?>
2
+ <package><name>KJ_BetterVisitorLog</name><version>1.0.0</version><stability>stable</stability><license>ASL</license><channel>community</channel><extends></extends><summary>Better Visitor Log</summary><description>A client-side replacement for the Magento visitor log</description><notes></notes><authors><author><name>Kalen Jordan</name><user>kalenjordan</user><email>kalen@magemail.co</email></author></authors><date>2015-06-19</date><time>8:49:28</time><compatible></compatible><dependencies><required><php><min>5.3.0</min><max>5.5.18</max></php></required></dependencies><contents><target name="mage"><file name="KJ-BETTER-VISITOR-LOG-README.md" hash="fbf325a82ad2d44a0adf4725108e89cb"/><dir name="app"><dir name="code"><dir name="community"><dir name="KJ"><dir name="BetterVisitorLog"><dir name="controllers"><file name="LogController.php" hash="e96834e936988aefa1498b9169647d6d"/></dir><dir name="etc"><file name="adminhtml.xml" hash="3ad44d451084b90aa5f2de2a0d2aaaf8"/><file name="config.xml" hash="ec852c3758da7fdef30669657095b78f"/><file name="system.xml" hash="e2f8adee28893c28fdba42f33dcdb6d4"/><file name="version.txt" hash="94e7f90b430ab8719ab3e660dfebce18"/></dir><dir name="Helper"><file name="Data.php" hash="6b1d72797826c73f8d8aef64b54c3484"/></dir><dir name="Model"><file name="Log.php" hash="5b35ffd949bb45f18bc1cf6aee563d70"/><dir name="Mysql4"><file name="Log.php" hash="cc19dae0ae2fcc857232bc3294856cab"/><file name="Setup.php" hash="f92b0d98fe6b9ea60596b216c32b0ef0"/></dir></dir><dir name="sql"><dir name="kj_bettervisitorlog_setup"><file name="install-1.0.0.php" hash="bf330aa6a7f84aa8c645a790b3c99262"/></dir></dir></dir></dir></dir></dir><dir name="design"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="kj_bettervisitorlog.xml" hash="520ba18e4332a0076503cb7597a2b092"/></dir><dir name="template"><dir name="kj_bettervisitorlog"><file name="footer.phtml" hash="fd656809863c2745821eb82ce52f3729"/></dir></dir></dir></dir></dir></dir><dir name="etc"><dir name="modules"><file name="KJ_BetterVisitorLog.xml" hash="4de04f2eb20d2dfc0755d249993e8384"/></dir></dir></dir></target></contents></package>