Add value from string to Google Ads as conversion value issue - iframe

On my website i generate leads via iframe forms. Based on the 'thank you page' i add an value to the url string like:
https//www.website.nl/thank-you?ids=5
the ids value of 5 is the value from the lead conversion (let's say $5,-)
with this script i push the value to Google Ads.
<?php if ( is_page(123) ) {
if (isset($_GET["ids"]) && (is_numeric($_GET["ids"])) ) {
$value = (float)str_replace(",",".",$_GET["ids"]);
$value = sprintf("%01.2f", $value);
}else{
$value = sprintf("%01.2f", 0);
}
echo '<script>
gtag("event", "conversion", {
"send_to": "AW-*****",
"value": '. $value .',
"currency": "EUR" });
</script>';
}?>
The above code worked great until my partner added automaticly a lead ID on the end of the url.
now the thank-you url is e.g.
https//www.website.nl/thank-you?ids=5&id=12345
Now my script won't work anymore...
how to fix?
any ideas?
probably i can't add my ids to the end of the url, because i need to fill in the thank-you url in the iframe static.
hope anyone can help me out to measure conversion values again and send them over to Google Ads :)

You should look at the way your partner is adding the new parameter. It should look something like this :
https//www.website.nl/thank-you?ids=5&id=12345
Read this for details about Query string structures.

Related

How to correctly add meta values as search keys using Wordpress REST API?

I currently have this a meta fields that are registered to the post, and another post type.
I was able to register the metafields correctly and I am able to do a POST for me articles through REST API.
What I am trying to figure out now is how can I add the meta key fields as search keys when I try to do a GET request through the rest API.
I searched through google but I am getting outdated resources.
I currently have this code:
$this->post_types = array ( 'post', 'events' );
foreach( $this->post_types as $field ) {
add_filter("rest_" . $field . "_query", function ($args, $query) {
$args["meta_query"] = "event_id";
$args["meta_query"] = "event_date";
return $args;
}, 10, 2);
}
But when I try to search using: /wp-json/wp/v2/events?filter[meta_query][event_id]=15432 I am getting incorrect results.
Is there something in my code that I missed? Thanks in advance for any help.

Get View Order URL for custom email in WooCommerce

I want to get the URL of the Order Details page from where user can see their order details, because using some 3rd party API I send mail to the customer after the product is shipped.
I tried this code
$order = wc_get_order(109);
$public_view_order_url = esc_url( $order->get_view_order_url() );
echo $public_view_order_url; //http://example.com/my-account/view-order/109/
but the URL generated by ^^ above code only works for logged in customer. Is it possible to get a Public URL so that user don't have to logged in because most of the customer don't have any account. I know for security reason email id and invoice number is needed.
One solution that I think is by creating an custom page which will accept order_id, order_key and email_id in GET parameter and query it and display the result; the whole thing I have to create but is there any WooCommerce function/hook for this?
I googled it and also spend time in Woo doc but the result was negative,
Any help or suggation will be very helpfull for me.
Thanks.
Not sure if I'm missing something, but it sounds like what you want can be achieved with the Order Received URL:
$order->get_checkout_order_received_url()
This would give you something like https://example.com/checkout/order-received/12345/?key=wc_order_1ab2cd3ef4g, which is a direct link to the order receipt shown after a successfully processed order.
This link will show billing & shipping details, order details & payment method by default, and doesn't require that the customer has an account or for them to be logged in.
I couldn't find a way to do that so I just put a condition in my email. If they have an account, link to the order, if they don't encourage them to make an account for the next order.
<p><?php
$email_order_url =$order->get_view_order_url();
if ( $order->get_user() ) {
_e( "Your message with <a href='" . $email_order_number . "'>Order
Number'</a>.", 'woocommerce' );
} else {
_e( "Message to encourage them to create an account on the regular
accounts page" , 'woocommerce' );
}
Everything you need is in post meta, this works for me when in a loop that can access get_the_ID():
<?php $the_order_id = get_the_ID();
$the_order_key = get_post_meta($the_order_id, '_order_key', true); ?>
https://yoursiteurl.com/checkout/order-pay/<?php echo the_order_id; ?>/?pay_for_order=true&key=<?php echo $the_order_key; ?>
Otherwise, if you can replaced get_the_ID() with the Order ID in some other way (however you're getting 109 in your initial question), that would work as well.

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.

Extend check for cookies by using the post ID

With the following code I set a cookie:
function cta_setcookie($data) {
//setcookie
setcookie('cta_'.COOKIEHASH, 'checked', time() + 3600, COOKIEPATH);
return $data;
}
and herewith I check if the cookie present:
//check whether user has cookie
$checked = ( !empty($_COOKIE['cta_'.COOKIEHASH]) && 'checked' == $_COOKIE['cta_'.COOKIEHASH] ) ? true : false;
It works, but with this method, the cookie is valid for all sites with my WordPress installation. What I need is to take it only to the actual post.
I tried to add the get_the_ID() function
setcookie('cta_'.COOKIEHASH.get_the_ID(), 'checked', time() + 3600, COOKIEPATH);
to get the cookie unique relative to the post. The cookie is set with the post id, but then the check won´t work. Any attempts failed after adding the get_the_ID(); to change the check :-(
How should the check be coded now?
get_the_ID() isn't available everywhere. You might assign it to a value, then echo that, to be sure that you actually have something to work with.
Like:
$myid = get_the_ID();
echo 'My id is '.$myid;
If that works, then you should be able to concatenate it to your cookie and check for that. IF it's null, then you aren't actually setting the unique value that you are expecting.

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources