Pass order data to AdForm tracking code in Woocommerce thankyou - javascript

I am looking to pass along the WooCommerce Variables "OrderID" and "Cart_total" into our Ad Tracking platform.
I was supplied with the tracking code which needed to be implemented and am having issues getting these variables into AdForm.
Here's the snippet, which works perfectly, I think that I am not including the variables correctly.
/**
* #snippet Add Conversion Tracking Code to Thank You Page
* #how-to Watch tutorial # https://businessbloomer.com/?p=19055
* #sourcecode https://businessbloomer.com/?p=19964
* #author Rodolfo Melogli
* #testedwith WooCommerce 3.3.4
*/
add_action( 'woocommerce_thankyou', 'bbloomer_conversion_tracking_thank_you_page' );
function bbloomer_conversion_tracking_thank_you_page() {
?>
<!-- Adform Tracking Code BEGIN -->
<script type="text/javascript">
window._adftrack = Array.isArray(window._adftrack) ? window._adftrack : (window._adftrack ? [window._adftrack] : []);
window._adftrack.push({
pm: XXXXXXXX,
divider: encodeURIComponent('|'),
pagename: encodeURIComponent('conversion'),
order : {
sales: '$woocommerce->cart->get_cart_total();',
orderid: '$order->get_id();'
}
});
(function () { var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://track.adform.net/serving/scripts/trackpoint/async/'; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); })();
</script>
<noscript>
<p style="margin:0;padding:0;border:0;">
<img src="https://track.adform.net/Serving/TrackPoint/?pm=XXXXXXX&ADFPageName=conversion&ADFdivider=|" width="1" height="1" alt="" />
</p>
</noscript>
<!-- Adform Tracking Code END -->
<?php
}
Specifically, I was asked to provide the following variables:
sales: '$woocommerce->cart->get_cart_total();',
orderid: '$order->get_id();'
But these are not working. I am unfortunately not a programmer (the programmer is on holiday). I think I need to call the variables somewhere.

There is no more cart data in Order received (thankyou) page, so you surely mean $order->get_total(). Also there is some mistakes in your code.
So you need:
first to get the WC_Order object from the missing $order_id argument in your hoked function
To get order total
To include and display the values in the javascript code.
Try the following:
add_action( 'woocommerce_thankyou', 'bbloomer_conversion_tracking_thank_you_page', 12, 1 );
function bbloomer_conversion_tracking_thank_you_page( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
?>
<!-- Adform Tracking Code BEGIN -->
<script type="text/javascript">
window._adftrack = Array.isArray(window._adftrack) ? window._adftrack : (window._adftrack ? [window._adftrack] : []);
window._adftrack.push({
pm: XXXXXXXX,
divider: encodeURIComponent('|'),
pagename: encodeURIComponent('conversion'),
order : {
sales: '<?php echo $order->get_total(); ?>',
orderid: '<?php echo $order->get_id(); ?>;'
}
});
(function () { var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://track.adform.net/serving/scripts/trackpoint/async/'; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); })();
</script>
<noscript>
<p style="margin:0;padding:0;border:0;">
<img src="https://track.adform.net/Serving/TrackPoint/?pm=XXXXXXX&ADFPageName=conversion&ADFdivider=|" width="1" height="1" alt="" />
</p>
</noscript>
<!-- Adform Tracking Code END -->
<?php
}

Related

Inserting Javascript into admin footer wordpress

I need to add Livechat widget on all WordPress admin pages (backend).
The Livechat JavaScript code is this:
<!-- Start of LiveChat (www.livechatinc.com) code -->
<script type="text/javascript">
window.__lc = window.__lc || {};
window.__lc.license = 12345678;
(function() {
var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})();
</script>
<noscript>
Chat with us,
powered by LiveChat
</noscript>
<!-- End of LiveChat code -->
Livechat has a plugin for the chat to work on the front-end pages. I actually wanted to add this code to every page of WordPress back-end.
I was thinking of putting it in the admin footer through the functions.php of my child theme, but I'm not sure how to put this code together.
The code I am inserting into the child theme is this:
function remove_footer_admin () {
echo 'My footer text. Thank you WordPress for giving me this filter.';
}
add_filter( 'admin_footer_text', 'remove_footer_admin' );
Where should I put this code to work?
You can add JavaScript code in admin footer using admin_footer hook.
If you also need to add same code on the front-end, then apply wp_footer hook too.
Here is a complete code goes in child theme's functions.php:
// Function to render LiveChat JS code
function lh_add_livechat_js_code() {
?>
<!-- Start of LiveChat (www.livechatinc.com) code -->
<script type="text/javascript">
window.__lc = window.__lc || {};
window.__lc.license = YOUR_KEY; // use your license key here
(function() {
var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})();
</script>
<noscript>
Chat with us,
powered by LiveChat
</noscript>
<!-- End of LiveChat code -->
<?php
}
add_action( 'admin_footer', 'lh_add_livechat_js_code' ); // For back-end
add_action( 'wp_footer', 'lh_add_livechat_js_code' ); // For front-end

Implementing disqus on a php blogging website

I have this code to include in my php website but I keep getting this error: We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
I have left trusted domains field empty in disqus dashboard to allow development on localhost.
The id is specific to each post.
<script type="text/javascript">
var disqus_config = function () {
var id = <?php echo $_GET['id']; ?>;
this.page.url = 'localhost/blog/viewimage.php?id='+id;
this.page.identifier = id;
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://ashexcreations.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>

Javascript - I have to use sleep function for run my function

I use youtube data api. I run my code, in that.
keyWordsearch()
It's not working. Chrome console say "Uncaught TypeError: Cannot read property 'setApiKey' of undefined"
But it is working strangely
setTimeout(keyWordsearch, 1000);
My all code
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="<?php echo SITE_PUBLIC; ?>/bootstrap-3.2.0/dist/js/bootstrap.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
<script>
$(document).ready(function()
{
var domain = '<?php echo SITE_URL; ?>';
function makeRequest(q, resultsIndex) {
var request = gapi.client.youtube.search.list({
q: q,
part: 'snippet',
type: 'video',
maxResults: 1,
videoCategoryId:10
});
request.execute(function(response) {
$('#results').empty()
var srchItems = response.result.items;
$.each(srchItems, function(index, item) {
vidTitle = item.snippet.title;
vidThumburl = item.snippet.thumbnails.default.url;
vidThumbimg = '';
html = '<div class="embed-responsive embed-responsive-16by9"> \
<iframe height="315" \
src="//www.youtube.com/embed/'+ item.id.videoId +'" \
frameborder="0" allowfullscreen></iframe> \
</div><br>'
$('.musics-results:eq('+resultsIndex+')').append(html);
})
})
}
function keyWordsearch(){
gapi.client.setApiKey('blabla');
gapi.client.load('youtube', 'v3', function() {
$('.musics-results').each(function( index, value){
data = jQuery.parseJSON( $(this).attr('value') );
$.each(data['names'], function( index2, value2 ) {
makeRequest(value2, index);
});
});
});
}
//setTimeout(keyWordsearch, 1000);
keyWordsearch();
})
</script>
What should i do? Thanks in advance
Here:
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
You are just loading the API client wrapper script; it may do background loading of additional scripts and will call the function you provided, googleApiClientReady, when it's done loading.
Without setTimeout, your keywordSearch function runs too soon, before the API client has actually been loaded. With setTimeout, you just got lucky that the client got loaded within those 1000 milliseconds, but this won't be the case on a slow connection.
So you should put your startup code into a function named googleApiClientReady (or change that name). See https://developers.google.com/api-client-library/javascript/samples/samples for working examples.

Getting Weird responseText

I've a very short php code written just for testing the ajax in a Cordova application.
Code for php is :
<?php
if (isset($_POST["TEST"])){
if ($_POST["TEST"] == "TEST"){
$resp["GOOD"] = "TEST WORKS!!";
echo (json_encode($resp));
}
}
?>
My request code is :
$.post('http://mobtest.bugs3.com/test.php',
{ TEST: 'TEST' },
function (result) {
console.log(result);
$('#txtlbl').text(result);
}
);
I am expecting the responseText to be "TEST WORKS!!" but what I get is :
{"GOOD":"TEST WORKS!!"}<!-- www.serversfree.com Analytics Code -->
<script src="http://www.serversfree.com"></script><noscript><a title="Free hosting servers" href="http://www.serversfree.com">Free servers</a><a title="Free websites hosting server" href="http://www.serversfree.com">Free websites hosting server</a><a title="Free hosting server features" href="http://www.serversfree.com/server-features/">Free server features</a><a title="Free hosting" href="http://www.bugs3.com">Free hosting</a><a title="Page rank" href="http://www.1pagerank.com">Page rank</a></noscript>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24425628-3']);
_gaq.push(['_setDomainName', window.location.host]);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" src="http://www.bugs3.com/ganalytics.js"></script>
<!-- End Of Analytics Code -->
I don't understand whats happening here.
The answer here might solve your problem:
https://stackoverflow.com/a/10768130/1696795
In short user cleong wrote:
The code is in a PHP auto-append file. It doesn't get executed if you exit explicitly instead of letting the script reach the end of the file.
so just append this to the end of your php code:
exit();
for some reason, you are trying to echo your whole array.
update: plus you use json_encode while trying to access the result directly in your javascript.
so just echo the variable in your php code:
echo $resp["GOOD"];

Disqus comment count not working

I installed Disqus manually in a WordPress theme (didn't work at all via the plugin). It works fine, but I'm having trouble displaying the comment count.
I think I'm doing a lot of things right tough:
The link on the /blog/ page looks as follows:
<i class="fa fa-comment"></i> 0
The comments.php:
<?php if (comments_open()) : ?>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'myshortname'; // Required - Replace example with your forum shortname
var disqus_identifier = 'dq-<?php echo get_the_ID(); ?> ';
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
alert(disqus_identifier);
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
As you can see I'm alerting the disqus_identifier and therefore can verify it's being set correctly (in this case dq-332 as above)
And I am loading the count.js globally with
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'myshortname'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
I only got it working under one (faulty) circumstance:
Before I tried it with the data-disqus-identifier it worked by appending #disqus_thread to the permalink without trailing slash, so e.g. http://mydomain.com/the-post-title#disqus_thread
The problem with that was that comments there wouldn't show up in http://mydomain.com/the-post-title/ and http://mydomain.com/the-post-title/#disqus_thread (same with the trailing slash, as generated by WordPress everywhere).
That's why I ended up with the identifier approach after all. Which doesn't have any effects so far. Would be happy about any type of hint. Cheers!
Was not able to solve it this way. Spent some more time investigating why the WordPress plugin didn't work. Worked it out in the end.
Note: you shouldn't have any relative URL plugins activated with disqus.

Categories