How to retrieve the goal name in Analytics using APIs - google-analytics

I'm trying to retrieve the name of my Analytics goals through the Google API using PHP. I tried this code but I don't understand why it creates a loop and does not return any value to me.
Anyone have any ideas?
Thanks so much
$goals = $analytics->management_goals->listManagementGoals('123456', 'UA-123456-1', '~all');
foreach ($goals->getItems() as $key) {
$html = <<<HTML
<pre>
Goal id = {$key->getId()}
Goal name = {$key->getName()}
HTML;
$html .= '</pre>';
print $html;
}

Related

How to create an ad using Facebook Marketing API for PHP?

I followed https://developers.facebook.com/docs/marketing-api/buying-api#ad-creative but it didn't work. I stuck at creating AdImage.
Api::init($facebook_appid, $facebook_appsecret, $facebook_accesstoken);
$image = new AdImage(null, $facebook_accountid);
$image->{AdImageFields::FILENAME} = '/path/to/file.jpg';
$image->create();
echo 'Image Hash: ', $image->{AdImageFields::HASH}, PHP_EOL;
I got:
$parent_id as a parameter of constructor is being deprecated, please try not to use this in new code.
But assigning account id to a field doesnt work either:
Api::init($facebook_appid, $facebook_appsecret, $facebook_accesstoken);
$image = new AdImage();
$image->{AdImageFields::ACCOUNT_ID} = $facebook_accountid;
$image->{AdImageFields::FILENAME} = public_path('demo/banners/coworking-300x250.png');
$image->create();
echo 'Image Hash: ', $image->{AdImageFields::HASH}, PHP_EOL;
I got:
A parent ID is required.
-- UPDATE --
The following should work for images:
Api::init($facebook_appid, $facebook_appsecret, $facebook_accesstoken);
$account = new AdAccount($facebook_accountid);
$image = $account->createAdImage([], [
AdImageFields::FILENAME => public_path('demo/banners/coworking-300x250.png'),
]);
The current problem is - how to create account for testing...
i have same problem with you. My problem solved by using setParentId() function from AbstractCrudObject.php file.
Do change your code like this, hope it helps
$image = new AdImage();
$image->setParentId("act_somenumber");

How to integrate weather api in wordpress

Please anyone help me how to integrate a api in wordpress api is in json formate
https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
Iam using this code but im not able to display temprature
<?php
$url = 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22';
$response = wp_remote_get( $url );
if( is_array($response) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
print_r(($body));
}
?>
Abu,
Put the code somewhere useful in wordpress so you can use the wp_remote_get
Understand the structure of the response (just putting the url in firefox address bar will give JSON structure
Understand that you receiving JSON, and decode it http://php.net/manual/en/function.json-decode.php
Find the temperature in the structure and use that as per code example below. Slightly freak out when you see the number but then ...
Read the API FAQ https://openweathermap.desk.com/customer/en/portal/articles/1996493-switching-between-temperature-units and realise the number is in Kelvin
I'll leave it to you to research how to request temp in Celcius or Fahrenheit
function show_temp() {
$url = 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22';
$response = wp_remote_get( $url );
if( is_array($response) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
$resp = json_decode($body);
echo 'The temp in Kelvin is: '.$resp->main->temp;
echo '<pre>'.print_r($resp, true).'</pre>';
}
}
add_shortcode ('show_temp', 'show_temp');

Adding a Dynamic Date as a Wordpress Function

I want to add a shortcode function in my Wordpress site that shows the date one week from now, two weeks from now and 3 days from now. I'd like to be able to add the shortcode [addoneweek] on pages where I need them.
Here is the code that I have, but it isn't working. What am I doing wrong?
function addoneweek( $aow )
{
return(<?php $now = new DateTime();
echo $now->add(new DateInterval('P1W'))->format('m-d-Y');
?>)
}
add_shortcode( 'arttime', 'addoneweek');
Try below code in your current functions.php
add_shortcode('arttime', 'arttime');
function arttime() {
$Today = date('d:m:y');
// add 3 days to date
$html = Date('d:m:y', strtotime("+3 days")) . '<br/>';
// add 7 days to date
$html .= Date('d:m:y', strtotime("+7 days")) . '<br/>';
// add 14 days to date
$html .= Date('d:m:y', strtotime("+14 days")) . '<br/>';
return $html;
}
Firstly, I would suggest reading over the add_shortcode codex entry. It will explain how to use attributes/arguments.
Secondly, you are returning on your third line, which means that the echo on the fourth line is not going to run.
Thirdly, you also have a random opening <?php tag after your return, which you then try to close on your fifth line.
Fourth, and this is not really a big problem, but you should actually return your end result, instead of echo'ing it. echo will still output your request, but you're then essentially running an echo inside and echo when the shortcode runs.
This is what your function should look like:
function addoneweek()
{
$now = new DateTime();
return $now->add(new DateInterval('P1W'))->format('m-d-Y');
}
add_shortcode( 'arttime', 'addoneweek');

Convert post permalink to real post id for live and stage site

I am in a mesh and need some help.
There are three actor evolved.
Live Site / Production Site (wordpress)
Local Site / Stage / Testing Site (wordpress)
Third party analytic
Background:
I know its very bad but the fact is that the production sites and local sites content are not synced. i.e post id 1 in production can be as post id 24 in local.
Problem Definition:
I was assigned to use the third party's API to grab the list of top post with maximum hits and show it in our website.
$result = file_get_contents($url);
$result_json = json_decode($result, true);
The above lines did that for me easily.
Error:
Since the 3rd party did not had post excerpt, they send URL, image_link, hit_count, post title and author information as JSON.
So on my site I was able to show all these fields very well. But the problem started when my manager added post excerpt to the list.
I tried converting URL to postid and got the post excerpt using:
get_the_excerpt(url_to_postid($top_posts['link']))
It worked for live site. but in the stage and local its not working.
Well post URL has its domain name. Even when I replaced the domain name with my local or stage domain. its not showing any thing. and for some post when it shows the excerpt its not from the same article.
Guys need some idea.
Is there some way that I can get slug from URL?
I tried using explode() function. But sometime the slug is not the last item in array.
Thank you for reading it all. I appreciate you help.
For now I came with a solution. Primarily Using the URL to get post ID, if not found using post title to get the post id.
public static function get_excerpt ( $id = null ) {
$post = get_post($id);
if ( empty($post) ) {
return '';
}
if ( strlen($post->post_excerpt) ) {
// Use the excerpt
$excerpt = $post->post_excerpt;
$excerpt = apply_filters('the_excerpt', $excerpt);
} else {
// Make excerpt
$content = $post->post_content;
$content = strip_shortcodes($content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
$excerpt = wp_trim_words($content, $excerpt_length, $excerpt_more);
$excerpt = wpautop($excerpt);
}
return apply_filters('wp_trim_excerpt', $excerpt);
}
Called this function to get the excerpt.
$postid = url_to_postid($top_posts['link']);
$postid = $postid!=0 ? $postid : get_page_by_title($post_title, OBJECT, 'post')->ID;
$excerpt = self::get_excerpt($postid);
This worked in my case as I didn't have posts with same title.
I am still waiting for some better solution to my problem.

Wordpress shortcode only returning 1 from feed despite using foreach

I am trying to create a custom Wordpress plugin which utilises shortcodes to output what I want. In this text code I am attempting to read an rss file and spit out just a list of the top 5 feeds.
The $showno is one of the shortcode variables so I can use the following [player show=foo snowno=5]. In the example code $show isn't used.
The code below only shows the most recent item in the feed list. If I change the return to echo then it works as expected except it show at the top of the post instead of where I've entered the shortcode. I searched for an answer to this and the solution offered was simply "use return" which I've done...
Appreciate advice. Thanks
include_once(ABSPATH . WPINC . '/rss.php');
$num_items = $showno;
$feedurl = 'http://feeds.bbci.co.uk/news/rss.xml';
$feed = fetch_rss($feedurl);
$items = array_slice ($feed->items, 0, $num_items);
foreach ($items as $item ) {
$title = $item[title];
$mp3link = $item[link];
$description = $item[description];
return "<li>$title - $description</li>";
}
Shortcodes should be returned not echoed.
In your code, you are breaking the execution of the foreach and returning the first value.
You should build a string variable and after the foreach loop return it, so all your loop will be included, e.g.:
$final_html = '';
foreach( $items as $item )
{
$final_html .= "<li>$title - $description</li>";
}
return $final_html;

Resources