Skip to content

Instantly share code, notes, and snippets.

@jloosli
Created March 2, 2012 22:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jloosli/1962059 to your computer and use it in GitHub Desktop.
Save jloosli/1962059 to your computer and use it in GitHub Desktop.
Google Analytics Ecommerce notification for Gravity Forms
<?php
function analytics_confirmation($confirmation, $form, $entry, $ajax = 0)
{
$isProduct = false;
foreach ($form['fields'] as $field) {
if ($field['type'] == 'product') {
$isProduct = true;
$fieldID = $field['id'];
$productSKU = $form['id'] . '-' . str_replace(" ", "_", $field['label']);
break;
}
}
if ($isProduct) {
if (in_array($entry['payment_status'], array('Approved', 'Processing'))) {
$id = $entry['id'];
$amt = ($entry['payment_amount'] != '') ? $entry['payment_amount'] : floatval($entry["$fieldID.2"]);
$js = <<<HTML
<script>
var _gaq = _gaq || [];
_gaq.push(['_trackPageview','/checkedout']);
_gaq.push(['_addTrans'
'{$id}',
'Store',
'{$amt}',
'',
'',
'',
'',
'',
''
]);
_gaq.push(['_addItem',
'{$id}',
'{$productSKU}',
'{$productSKU}',
'',
'{$amt}',
'1'
]);
_gaq.push(['_trackTrans']);
</script>
HTML;
if (is_array($confirmation)) {
$redirect = $confirmation['redirect'];
$confirmation = $js;
$confirmation .= "\n<script>window.location='$redirect'</script>";
} else {
$confirmation .= $js;
}
}
}
return $confirmation;
}
@jg314
Copy link

jg314 commented Oct 21, 2013

This is exactly what we're looking for. Where are you calling this function from? What hook are you using in Gravity Forms?

@honzapav
Copy link

@jg314 have found your answer? I am dealing with the same. Thx

@alxanthony
Copy link

@honzapav, What did you end up doing? I can't seem to find anything, and Gravity Forms just plays stupid about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment