How to get unique visists per day using GAPI and Google Analytics? - google-analytics

I have a very simple custom report in Google Analytics that lists Date, Visits, Unique Visitors and Pageviews for a certain month.
I am trying to duplicate this very simple report using GAPI, but I cannot find a simple way to add Unique Visitors to my table.
See my code below. How can I modify this code to show Unique Visitors?
$profile_id = "xxxxxxxx";
$report_id = "xxxxxxxx";
$dimensions = array('date');
$metrics = array('pageviews','visits');
$sort_metric = array('date');
$filter = null;
$start_date = '2013-02-01';
$end_date = '2013-02-28';
$start_index = 1;
$max_results = 10000;
echo '<table>';
echo '<tr>';
echo '<th>Date</th>';
echo '<th>Visits</th>';
echo '<th>Unique Visitors</th>';
echo '<th>Page Views</th>';
echo '</tr>';
$ga = new gapi('xx#xxxxxxxxx.com','xxxxxxxx');
$ga->requestReportData($profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results);
foreach($ga->getResults() as $result)
{
echo '<td>' . $result->getDate() . '</td>';
echo '<td>' . $result->getVisits() . '</td>';
echo '<td>' . '?' . '</td>';
echo '<td>' . $result->getPageviews() . '</td>';
echo '</tr>';
}
echo "</table>";

The metric is called visitors and no visitor and you have to change the dimension to userDefinedValue
$profile_id = "xxxxxxxx";
$report_id = "xxxxxxxx";
$dimensions = array('userDefinedValue');
$metrics = array('pageviews','visits','visitors');
$sort_metric = array('date');
$filter = null;
$start_date = '2013-02-01';
$end_date = '2013-02-28';
$start_index = 1;
$max_results = 10000;

The metric is called "visitor". Try adding it to your report.

$profile_id = "xxxxxxxx";
$report_id = "xxxxxxxx";
$dimensions = array('date');
$metrics = array('pageviews','visits','visitors');
$sort_metric = array('date');
$filter = null;
$start_date = '2013-02-01';
$end_date = '2013-02-28';
$start_index = 1;
$max_results = 10000;

Related

change price based on qty in grouped woocommerce products

My need is to display gross weight and net weight of the products and change the gross weight and net-weight based on the quantity. While change the qty the net weight and gross-weight also should change. It was working fine in single product page while coming to the grouped products unable to achieve this.
Here is my single product working fine: https://hamarafresh.com/product/salmon/
Here is my grouped products not working: https://hamarafresh.com/product/lady-fish/
I am getting gross weight and net weight from custom field.
add_action( 'woocommerce_after_quantity_input_field', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
if ( is_product() ) {
global $woocommerce, $product;
$gross_weight = get_post_custom_values($key = 'gross_weight');
$net_weight = get_post_custom_values($key = 'net_weight');
get_post_meta( $post_id, $key, $single );
// let's setup our divs
?>
<?php $netweight5= esc_html( get_post_meta( get_the_ID(), 'net_weight', true ) ); ?>
<?php $grossweight5= esc_html( get_post_meta( get_the_ID(), 'gross_weight', true ) ); ?>
<?php
echo sprintf('<div id="net_weight_disp" style="margin-bottom:10px; margin-top:7px;">%s %s</div>',__('Net Weight:'),'<span class="price">'.$netweight5.'</span>');
echo sprintf('<div id="gross_weight_disp" style="margin-bottom:10px; margin-top:7px;">%s %s</div>',__('Gross Weight:'),'<span class="price">'.$grossweight5.'</span>');
echo sprintf('<div id="product_total_price" style="margin-bottom:20px; text-align: center;">%s %s</div>',__('Product Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
<?php $net_weight = get_post_custom_values($key = 'net_weight'); ?>;
<?php $netweight5= esc_html( get_post_meta( get_the_ID(), 'net_weight', true ) ); ?>;
var net_weightt = <?php echo $netweight5; ?>;
<?php $gross_weight = get_post_custom_values($key = 'gross_weight'); ?>;
<?php $grossweight5= esc_html( get_post_meta( get_the_ID(), 'gross_weight', true ) ); ?>;
var gross_weightt = <?php echo $grossweight5 ?>;
$('[name=quantity]').change(function(){
if (!(this.value < 0.5)) {
var net_weighttt = (net_weightt* this.value);
var gross_weighttt = (gross_weightt* this.value);
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
$('#net_weight_disp .price').html( currency + net_weighttt.toFixed(2));
$('#gross_weight_disp .price').html( currency + gross_weighttt.toFixed(2));
}
});
});
</script>
<?php
}
}
Change this line $('[name=quantity]').change(function(){
like below
`$("input[name^='quantity']").change(function(){`
Updated Answer
$("input[name^='quantity']").change(function() {
if (!(this.value < 0.5)) {
if ($(".woocommerce-grouped-product-list-item").length > 0) {
var nw = $(this).closest("tr").find(".net_weight_disp").data("netweight");
var cw = $(this).closest("tr").find(".gross_weight_disp").data("grossweight");
var pdt = $(this).closest("tr").find(".product_total_price").data("price");
var net_weighttt = (nw * this.value);
var gross_weighttt = (cw * this.value);
var product_total = parseFloat(pdt * this.value);
$(this).closest("tr").find('#product_total_price .price').html(currency + product_total.toFixed(2));
$(this).closest("tr").find('#net_weight_disp .price').html(net_weighttt.toFixed(2));
$(this).closest("tr").find('#gross_weight_disp .price').html(gross_weighttt.toFixed(2));
} else {
var nw = $(this).closest(".right_summary_content").find(".net_weight_disp").data("netweight");
var cw = $(this).closest(".right_summary_content").find(".gross_weight_disp").data("grossweight");
var pdt = $(this).closest(".right_summary_content").find(".product_total_price").data("price");
var net_weighttt = (nw * this.value);
var gross_weighttt = (cw * this.value);
var product_total = parseFloat(pdt * this.value);
$(this).closest(".right_summary_content").find('#product_total_price .price').html(currency + product_total.toFixed(2));
$(this).closest(".right_summary_content").find('#net_weight_disp .price').html(net_weighttt.toFixed(2));
$(this).closest(".right_summary_content").find('#gross_weight_disp .price').html(gross_weighttt.toFixed(2));
}
}
});
Because grouped products name attribute will be array.

display the picture of the products category

I want to display the picture of the products category on the map.
I want to show the category image
I tried many of the following WordPress functions and when I got vardump it gave me an array
this is the code of my map
In this section I want to show the image instead of the icon
<?php
$args = array('post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1);
$wp_query = new WP_Query($args);
?>
<?php if ($wp_query->have_posts()) :
$posts = $wp_query->posts;
$GLOBALS['all_ads']=$posts;
$lenght = count($posts); ?>
<ul class="row items ad-listing">
<?php for ($i = 0; $i < $lenght; $i++) {
$single_post = $posts[$i];
$terms = wp_get_post_terms($single_post->ID, 'city_categories', array('order' => 'DESC'));
$parent_id = $terms[0]->parent;
$parent_id_1 = $terms[1]->parent;
$city_term = get_term_by('id', $parent_id, 'city_categories');
$name = $city_term->name;
$term_id = $terms[0]->term_id;
$term_id_two = $terms[1]->term_id;
$ad_type = get_post_meta($single_post->ID, 'ad_type','true');
$parent_id = $parent_id ? $parent_id :$parent_id_1;
if ($city_term_id == $term_id || $city_term_id == $term_id_two){
$data['lat'] = get_post_meta($single_post->ID, '_ad_latitude', true);
$data['long'] = get_post_meta($single_post->ID, '_ad_longitude', true);
$phone = get_post_meta($single_post->ID , 'shmarhtmascabt_v_shmarhtmascabt_m' , true);
$address = trim(get_post_meta($single_post->ID , 'adrs_v_shmarhtmascabt_m' , true));
$type = wp_get_post_terms($single_post->ID, 'product_cat')[0]->name;
$title = $single_post->post_title;
$ad_link = get_permalink($single_post->ID);
$sc_studi_cat_icon = get_term_meta($term_id->term_id, array( 1680,470 ), true);
print_r($sc_studi_cat_icon);
$img_cat=wp_get_attachment_image($sc_studi_cat_icon,array('71', '76'));
$src = wp_get_attachment_image_src( get_post_thumbnail_id($single_post->ID), '', false, '' );
$sc_studi_cat_icon = get_term_meta($type->term_id, 'sc_studi_cat_icon', true);
$img_cat=wp_get_attachment_image($sc_studi_cat_icon,array('71', '76') , false , 'src');
var_dump($img_cat);
$data['text']="<div class='map-div-container'><div><a href='{$ad_link}'><h4>{$title}</h4></a></div>";
if ($phone){$data['text'].="<div><i class='fas fa-phone' style='font-size: 10px;'></i><b>تلÙÙ†:</b> {$phone}</div>";}
if ($address){$data['text'].="<div><i class='fas fa-map-marker-alt'></i><b>آدرس:</b> {$address}</div>";}
if ($type){$data['text'].="<div><i class='far fa-folder'></i><b>نوع:</b> {$type}</div></div>";}
$data['text']=urldecode($data['text']);
$map_data[$single_post->ID] = $data;
}
if (($state_term_id === $parent_id || $state_term_id[0] === $parent_id) && ($ad_type == 1936)):
It will be a great help if you could guide me how to do this.
Thanks in advance.
$type3 = wp_get_post_terms($single_post->ID, 'product_cat')[0];
$category_thumbnail = get_woocommerce_term_meta($type3->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);

How to get the first cell that has a value in phpexcel

I have an excel file that have first data in cell B4, this is the first of the content area. The content area range is B4:D9.
How can I get the B4?
I used this code but it is not getting the b4,
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$data = array();
$highestNum = 0;
for ($row = 1; $row <= $highestRow; $row++){
$data[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
"",
TRUE,
FALSE);
$columns[] = PHPExcel_Cell::stringFromColumnIndex($row-1);
$highestNum = $row;
}

Fatal error: Call to a member function query() on a non-object in wordpress

I'm trying to insert a new table row into a custom wpdb table. The data is coming from a form posted to a script.
I'm declaring global $wpdb at the start of the script, and inserting into the db, but for some reason it doesn't like the query function, which WordPress recommends to use in the codex.
i got this error Fatal error: Call to a member function query()
this is my code:
<?php
global $wpdb;
if($_POST){
$post_id = $_POST['post_id'];
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$tell = $_POST['tell'];
$date_m = $_POST['date_m'];
$date_y = $_POST['date_y'];
$consent = $_POST['consent'];
$check_error = 0;
if (!isset($name) || $name == ''){
echo "empty";
$check_error = 1;
}
elseif (!isset($email) || $email == ''){
echo "empty";
$check_error = 1;
}
$name = mysql_real_escape_string($name);
$email = mysql_real_escape_string($email);
$city = mysql_real_escape_string($city);
$tell = mysql_real_escape_string($tell);
$date_m = mysql_real_escape_string($date_m);
$date_y = mysql_real_escape_string($date_y);
$consent = mysql_real_escape_string($consent);
$query = "INSERT INTO {$wpdb->prefix}kishvote SET ";
$query .= "post_id = '" . $post_id . "', ";
$query .= "date_timevote = '" . date( 'Y-m-d H:i:s' ) . "', ";
$query .= "u_name = '" . $name . "', ";
$query .= "u_city = '" . $city . "', ";
$query .= "u_mail = '" . $email . "', ";
$query .= "u_tell = '" . $tell . "', ";
$query .= "date_m = '" . $date_m . "', ";
$query .= "date_y = '" . $date_y . "', ";
$query .= "consent = '" . $consent . "', ";
$success = $wpdb->query( $query );
if ($success) {
$error = 0;
echo "submit";
} else {
$error = 1;
echo 'eror dade';
}
}
?>

simple html dom - cannot redeclair str_get_html()

Im trying to make a class to help with SEO and also compare google API with actual results
class:
<?php
class true_seo {
public $string, $amount;
private $arr;
public function __construct(){}
public function set_g_key( $key ) {
$this->g_key = $key;
}
public function set_phrase( $string ){
if( is_string ( $string ) ) {
$string = array( $string );
}
if( is_array ( $string ) ) {
$this->phrases = $string;
}else{
Throw new exception("incorect input for phrase, string or array");
}
}
public function get_sites_use_spider( $amount ) {
require "simple_html_dom.php";
$main_result = array();
foreach( $this->phrases as $phrase ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => 0, "maxResults" => $amount, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
$data = true_seo::google_search_api( $APIparams, 'http://www.google.co.uk/search', false );
new simple_html_dom();
$html = str_get_html( $data );
$result = array();
foreach( $html->find('li.g h3 a') as $g ) {
$data = $g->parent()->nextSibling();
$other = $data->find('span a');
$x = 0;
foreach( $other as $d ) {
( $x == 0 ? $cache = $d->href : $simular = $d->href );
$x++;
}
$excess_span = $data->find('span',0)->outertext;
if( isset( $data->find('div',0)->tag ) ) {
$excess_div = $data->find('div',0)->outertext;
$title = str_replace( array( $excess_span, $excess_div, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','','' ), $data->outertext );
}else{
$title = str_replace( array( $excess_span, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','' ), $data->outertext );
}
$result[] = array( 'link' => $g->href, 'title' => strip_tags( $title ), 'cache' => $cache, 'simular' => 'http://www.google.co.uk' . $simular );
}
$main_result[$phrase] = $result;
$html->clear();
}
$this->non_api_data = $main_result;
}
public function get_sites_use_api( $amount ) {
$arr = array();
foreach( $this->phrases as $phrase ) {
if( $amount > 4 ) {
$times = $amount / 4;
}else{
$times = 1;
}
$arg = array();
for($x = 0; $x < $times; $x++ ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => ($x * 4), "maxResults" => 4, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
if( $data = true_seo::google_search_api( $APIparams, 'http://ajax.googleapis.com/ajax/services/search/web' ) ) {
$arg = array_merge($arg, $data->responseData->results);
}else{
Throw new exception("Request error: no results returned from Google.");
}
}
$arg = array_reverse( $arg );
$remove = $amount % 4;
if( $amount < 4 ) {
$remove = 4 - $amount;
}
for( $x=0; $x < $remove; $x++ ) {
unset( $arg[$x] );
}
$arg = array_reverse( $arg );
foreach( $arg as $g ) {
$result = array( 'link' => $g->url, 'title' => strip_tags( $g->content ), 'cache' => $g->cacheUrl, 'simular' => 'na' );
$arr[$phrase][] = $result;
}
}
$this->api_data = $arr;
}
public function google_search_api($args, $url, $api = true){
if ( !array_key_exists('v', $args) ) {
$args['v'] = '1.0';
}
$url .= '?'.http_build_query($args, '', '&');
if( $result = #file_get_contents($url) ) {
if( $api == true ) {
return json_decode($result);
}else{
return $result;
}
}else{
Throw new exception("No data returned from url: $url");
}
}
public function set_get_actual( $string ) {
$this->actual->name = $string;
$this->actual->data = file_get_contents( $string );
}
public function get_actual_description(){
require_once "simple_html_dom.php";
new simple_html_dom();
$html = str_get_html( $this->actual->data );
return $html->find('head meta[name=description]',0)->content;
$html->clear();
}
}
?>
called by :
<?php
try{
require "./classes/class_true_seo.php";
$seo = new true_seo();
$seo->set_g_key('ABQIAAAAsWzmZ4RXdIk0a-LqpqKCBRSl_WmKnmsXGmN0kkjN2wkrfEOY-hT2sL-_x5v4NtT3DgElKNsR7FDJDQ');
$seo->set_phrase(array("web design mansfield"));
$seo->get_sites_use_api(10);
ob_start();
foreach( $seo->api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
$seo->set_get_actual( $results['link'] );
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
#echo "<p>" . $seo->get_actual_title() . "</p>";
echo "<p>" . $seo->get_actual_description() . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('p') . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('h2') . "</p>";
}
}
$api_return = ob_get_clean();
ob_start();
$seo->get_sites_use_spider(10);
foreach( $seo->non_api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
}
}
$non_api_return = ob_get_clean();
}catch(Exception $err){
$error = $err->getMessage();
}
?>
My problem being that I keep getting the error:
Fatal error: Cannot redeclare file_get_html() (previously declared in C:\wamp\www\seo\classes\simple_html_dom.php:37) in C:\wamp\www\seo\classes\simple_html_dom.php on line 41
which is due to the last function in the class get_actual_description().
Can anyone see where im cocking up?
regards,
Phil
put require_once "simple_html_dom.php" outside of function

Resources