0

The AdWords Documentation on how to Track transaction-specific conversion values states a PHP code as such:

<!-- Google Code for Purchase Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1234567890;
var google_conversion_language = "en";
var google_conversion_format = "1";
var google_conversion_color = "666666";
var google_conversion_label = "xxxxXXx1xXXX123X1xX";
if (<? echo $totalValue ?>) {
    var google_conversion_value = <? echo $totalValue ?>;
    var google_conversion_currency = <? echo $currency ?>;
}
    var google_remarketing_only = false;
/* ]]> */ 
</script>
<script type="text/javascript"
src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
    <div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt=""
src="//www.googleadservices.com/pagead/
conversion/1234567890/?value=
<? echo $totalValue ?>&amp;currency_code=<? echo $currency ?>
&amp;label=xxxxXXx1xXXX123X1xX&amp;guid=ON&amp;script=0">
    </div>
</noscript>
</body>

This indicates to me two things:

  1. We need to put the total order value in a $totalValue variable and the currency value into $currency.
  2. It should be placed at the end of the </body> on the page.

Aside from creating your own custom 'Thank You' page on Order Completion and adding it where you want in a custom page template, what's the best way to hook onto the default 'Thank You' page?

The WooCommerce Documentation has something on Custom tracking code for the thanks page that just simply hooks onto woocommerce_thankyou. Minified it looks like:

add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {

// Lets grab the order
$order = wc_get_order( $order_id );

// Do whatever else
}

But doesn't mention AdWords specifically.

I know through my own testing, that once we define $order = wc_get_order( $order_id ); we can get our variables simply using:

$totalValue = $order->get_total();
$currency = $order->currency;

This would bring about a complete function like so:

add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {

$order = wc_get_order( $order_id );
$totalValue = $order->get_total();
$currency = $order->currency;
?>

<!-- Google Code for Purchase Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1234567890;
var google_conversion_language = "en";
var google_conversion_format = "1";
var google_conversion_color = "666666";
var google_conversion_label = "xxxxXXx1xXXX123X1xX";
if (<?php echo $totalValue ?>) {
    var google_conversion_value = <?php echo $totalValue ?>;
    var google_conversion_currency = <?php echo $currency ?>;
}
    var google_remarketing_only = false;
/* ]]> */ 
</script>
<script type="text/javascript"
src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
    <div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt=""
src="//www.googleadservices.com/pagead/
conversion/1234567890/?value=
<?php echo $totalValue ?>&amp;currency_code=<?php echo $currency ?>
&amp;label=xxxxXXx1xXXX123X1xX&amp;guid=ON&amp;script=0">
    </div>
</noscript>
<?php
}

But does not allow us to put it at the end of the </body> as the AdWords Documentation states.

I can't find any information on Transaction Specific values like this in WooCommerce for AdWords, at least nothing definite. There are plenty of plug-ins but they all seem a bit overkill (correct me if I'm wrong) if it's as simple as adding a function perhaps such as the one above.

My Questions Are:

  • Would the above function work just fine regardless?
  • Is there anyway to place the function directly directly above the </body> as Google suggests?
  • Is there anything else I might be missing here for a proper configuration?
2

As the developer of the WooCommerce AdWords Conversion Tracking plugin I can give you a few answers to your questions plus some reasons to use a plugin or ours in particular.

Answers to your questions:

  1. Your function probably will be impaired or not working at all. The reason is, that WordPress filters out the CDATA tags automatically (everything within the content part that is between <body></body>). And you can't do anything about it. The filter can't be turned off. It is a bug that has been reported long time ago: http://core.trac.wordpress.org/ticket/3670 There is an experimental workaround, but it likely will not work with all themes.

  2. In theory yes. But Googles instruction is more of a recommendation. The tag will work regardless. If you put it in the header, or footer or directly after the tag, it doesn't really matter. And it is a quite difficult task to position it so exactly using WordPress if you don't want to edit your theme template files.

  3. Yes. You are missing: Deduplication, suppression of tracking for failed payments, tax and shipment exclusion, avoid tracking of admins and shop managers to start with.

Deduplication: If the visitor reloads the thankyou page for whatever reason, the order will be tracked twice (or even more times).

Failed payments: If a payment fails the thankyou page will still be triggered and thus your tracking code.

Tax and shipment: Generally you don't want to track shipment and tax, just the product price. To get the value without tax and shipment you'll have to use get_subtotal().

In our plugin we have solutions for all issues mentioned above. And we are working on even more interesting functions.

In general plugins can be a good thing if they go beyond just including simple functions, use best practices to avoid performance drag and keep the installations safe.

  • That is peculiar, thanks for the insight and well informed response. So by reading your response and reading that WordPress ticket about CDATA, it seems like an absolute minified, uncustomized possible function would look something like this; pastebin.com/Af3jhFFP ? – bbruman Jul 29 '17 at 13:57
  • Or something like this gist.github.com/devinsays/… if what you're saying right to put it in the footer because the CDATA isn't filtered there? I know I'm going against your advice, and I love the input from a plug-in creator, but I'm just trying to find even the lowest possible solution as there doesn't seem to be much info out there about this specifically. Custom vs. Plug-in is an interesting debate though and you definitely made some good points. – bbruman Jul 29 '17 at 14:46
  • 1
    Both solutions are valid and do work (in theory. I haven't tested your code. I've tested both in our plugin and they do work. But for the sake of maximum compatibility we chose a different path). If you were a plugin author then there would be a risk by using the footer hook to render it incompatible with certain themes. I've seen that before. – alev Jul 29 '17 at 15:44
1

From the documentation you cited for AdWords: "When inserting the tag, you'll place it on the static portion of the page, found within the section." It doesn't state it must be directly above the closing body tag, and is just providing example code for different common languages. It's a common thought since Google used to recommend Google Analytics scripts to be placed directly above the closing body tag, but for those it's recommended to be placed in the head now. So yes, what you have should work!

In terms of things you might be missing? Well there's quite a bit overall that could happen from point A to point B in a user's journey - but that's kinda one of the points of using a plugin. Plugins aren't necessarily huge overhead if they focus on doing one thing and doing it right.

Maybe as the admin or developer on the site, you don't want to have your tests through the system tracked. Throwing your code in there you'd have to include some additional logic to handle that.

Failed payments and duplicate transactions definitely occur on online shops frequently, so you don't want to have those tracked in those circumstances as well.

As you stumble across those circumstances over time, you end up writing up more code to handle them when you could have saved yourself a lot of worry and effort when things go wrong by just using proven working solutions!

  • Good advice, thank you. I believe there is a way to track failed payments so yes it's all about customization now (assuming it works I've yet to test - part of the reason I asked the question). Not sure if I agree on your last point.. it depends if you really want to learn what's going on internally. I might agree on bigger customizations/additions but not sure in this case. I could be wrong. – bbruman Jul 28 '17 at 3:20
  • I'll accept your answer by the way just would like to leave it open in case anyone else has any input. – bbruman Jul 28 '17 at 3:22
  • Yeah, I definitely understand your point of view (and agree) - it isn't as important here for something small, but definitely with larger and more complex things. Crack open a couple of the plugins that do what you're trying to do and read over the code and see what situations they do and don't cover. Maybe the flow isn't user friendly, or you find they really do have a bunch of extra junk, so you would have the opportunity to make something much better while still learning from the knowledge they have gained over time from feedback and bugs. – Tim Elsass Jul 28 '17 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.