';
}
}
}
}
// echo page output
$this->styleRenderer();
if (!empty($trackingCode) && !empty($accessToken)) {
$this->iframePage();
} else {
if (!empty($error)) {
echo $error;
}
$this->optionsPage();
}
$this->renderNotifyScript();
}
/**
*
*/
public function optionsPage() {
echo '
';
echo '
8digits Options
';
echo '';
echo '
';
}
/**
* Renders notification script
* This is for 8digits to be notified on new installations
* and offer support to find the best ways to run solutions
* for your site.
*/
public function renderNotifyScript() {
$notified = get_option('eightdigits_installation_notified');
if (!($notified)) {
$output .= <<
if(typeof jQuery!="undefined") {
addLoadEvent(function() {
jQuery.ajax({
url: '$this->_8digitsInterface/wordpress/installed',
method: 'POST',
dataType: 'jsonp',
data: {
siteurl: '$siteUrl',
email: '$adminEmail'
}
})
}());
}
EOD;
update_option('eightdigits_installation_notified', true);
}
echo $output;
}
public function iframePage() {
$adminEmail = get_option('admin_email');
$siteUrl = get_option('siteurl');
$trackingCode = get_option('eightdigits_tracking_code');
$accessToken = get_option('eightdigits_access_token');
$output = '';
echo $output;
}
/**
* Renders section header for settings
*/
public function accountSettingsSectionRenderer() {
$output = '
To activate 8digits, type in your tracking code and access token, and save changes.
';
$output .= '
';
$output .= '
If you have not registered with 8digits yet please sign up now
';
$output .= '
If you already have an account but you do not remember your tracking code or access token please visit your integration page
';
$output .= '
';
echo $output;
}
/**
* Renders input box for option
*/
public function textFieldRenderer() {
$args = func_get_args();
$args = $args[0];
$id = $args['id'];
$label = $args['label'];
echo ' ' . $label;
}
/**
* Renders hidden input box for option
*/
public function hiddenFieldRenderer() {
$args = func_get_args();
$args = $args[0];
$name = $args['name'];
$defaultVal = $args['default'];
$optVal = get_option($name);
echo ' ';
}
/**
* Adds 8digits integration code to footer. Also, renders scraping code to get called when 8digits JS SDK is ready.
*/
public function add8digitsCode() {
$trackingCode = get_option('eightdigits_tracking_code');
$output = '';
if($trackingCode) {
if($this->_extraCodeBefore) {
$output .= $this->_extraCodeBefore;
}
$version = self::$version;
$output .= <<
var _trackingCode = '$trackingCode';
(function() {
var wa = document.createElement('script'); wa.type = 'text/javascript'; wa.async = true;
wa.src = '$this->_8digitsStaticInterface/automation.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(wa, s);
})();
EOD;
}
echo $output;
}
/**
* Creates scraping code according to page. Currently, we are handling cart, checkout and congrats pages.
*
* @param $post
*/
public function view($post) {
global $woocommerce;
if(is_shop()) {
} else if(is_product_category()) {
} else if(is_product_tag()) {
} else if(is_product()) {
} else if(is_cart()) {
$cartItems = $woocommerce->cart->get_cart();
if (empty($cartItems) || !is_array($cartItems)) break;
$dataLayer = array(
'price' => "".$woocommerce->cart->total,
'itemCount' => "".$woocommerce->cart->get_cart_contents_count(),
'products' => array()
);
$products = array();
foreach($cartItems AS $key => $item) {
$product = $item['data'];
if (empty($products[$product->get_sku()])) {
$terms = get_the_terms( $product->id, 'product_cat' );
$categories = array();
foreach ( $terms as $term ){
$categories[] = $term->name;
}
// Build all fields the first time we encounter this item.
$products[$product->get_sku()] = array(
'name' => $product->get_title(),
'sku' => $product->get_sku(),
'category' => implode('|',$categories),
'price' => (double)number_format($product->get_sale_price(),2,'.',''),
'quantity' => (int)$item['quantity']
);
} else {
// If we already have the item, update quantity.
$products[$product->get_sku()]['quantity'] += (int)$item['quantity'];
}
}
// Push products into main data array.
foreach ($products as $product) {
$dataLayer['products'][] = $product;
}
// Trim empty fields from the final output.
foreach ($dataLayer as $key => $value) {
if (!is_numeric($value) && empty($value)) unset($dataLayer[$key]);
}
if (!empty($dataLayer)) {
$attributeList = json_encode($dataLayer);
$this->_extraCodeBefore = <<
var EightDigitsData = $attributeList;
function EightDigitsReady() {
EightDigits.setAttributes($attributeList);
setTimeout(function() {
EightDigits.event({ key: 'CartDisplayed', noPath: true });
}, 500);
}
EOF;
}
} else if(is_checkout()) {
$this->_extraCodeBefore = <<
function EightDigitsReady() {
EightDigits.event({ key: 'CheckoutDisplayed', noPath: true });
}
var attributeNamesMap = {
'billing_first_name': 'firstName',
'billing_last_name': 'lastName',
'billing_company': 'company',
'billing_email': 'email',
'billing_phone': 'phone'
};
jQuery(function() {
jQuery('.woocommerce-billing-fields').find('input[type="text"]').on('blur', function() {
var el = jQuery(this);
var id = el.attr('id');
var value = el.val();
if(attributeNamesMap.hasOwnProperty(id)) {
var attributeName = attributeNamesMap[id];
EightDigits.setAttribute({
name: attributeName,
value: value
});
}
})
})
EOF;
} else if(is_account_page()) {
} else if(is_order_received_page()) {
$this->_extraCodeBefore = <<
function EightDigitsReady() {
EightDigits.event({ key: 'OrderReceivedDisplayed', noPath: true });
}
EOF;
}
}
}
EightDigits::instance();
}
?>