Analytics.js not adding transaction.item to GA by cron job by affiliate network

160 views
Skip to first unread message

Ankur Gandhi

unread,
Jan 8, 2014, 12:00:22 PM1/8/14
to google-analyti...@googlegroups.com
Hi, When any user visited my site I generate clientId and a random unique subid and save both to DB. Then that subid send to affiliate network and they save it with transaction if there will be transaction. Now to add these transactions into Google Analytics E commerce they call one link like this http://example.com/conversions.php?subid=5b1300507cf376f065062214156d618c&storename=ANKURSTORE&orderid=ANKUR111&total=11.11 by adding necessasry data to link by cron job.

Now when that link called Here is process which happen in that file conversions.php :
1. Get subid and then fetch clientId for that subid from database.
2. Get other details by using $_GET.
3. Add it to Google Analytics by addtransaction and addItem

Here is code of conversion.php :

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-3970XXXX-Y', 'example.com');
</script>
<?php
    $subid = $_GET["subid"];
    $orderid = str_replace("#","",$_GET["orderid"]); // Order ID - unique ID for the transaction
    $amt = $_GET["total"]; //Total and unit price - affiliate fee for the sale
    $num_decimals = (intval($amt) == $amt) ? 0 :2;
    $total = number_format($amt,$num_decimals);
    $storename = urlencode(str_replace("#","",$_GET["storename"]));  // Affiliation or store name - name of the business your promoting (e.g. "WP4FB")
    $client_id = $wpdb->get_var("SELECT `ga` FROM `{$table_name}` WHERE `subid` = '{$_GET['subid']}';");
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs($ga_array1,$orderid1,$storename1,$total1) {
return <<<HTML
ga('set', 'clientId', '{$ga_array1}');
ga('ecommerce:addTransaction', {
'id': '{$orderid1}',
'affiliation': '{$storename1}',
'revenue': '{$total1}'
});
HTML;
}

// Function to return the JavaScript representation of an ItemData object.
function getItemJs($orderid2,$storename2,$total2) {
  return <<<HTML

ga('ecommerce:addItem', {
  'id': '{$orderid2}',
  'name': '{$storename2}',
  'price': '{$total2}',
  'quantity': '1'
});
HTML;
}
    ?>
<script>
ga('require', 'ecommerce', 'ecommerce.js');
<?php
echo getTransactionJs($ga_array,$orderid,$storename,$total);
echo getItemJs($orderid,$storename,$total);
?>
ga('ecommerce:send');
</script>


When this link called by affiliate network it is not adding transactions to Google Analytics. But if we manually run into browser then it works perfectlly.

pls I need help with this issue. I am trying it for couple of days. But didnt find anything and now this group is my last hope.

Thanks in advance. If you need any other info let me know.

Regards,
Ankur

Lukas Grebe

unread,
Jan 9, 2014, 5:47:05 AM1/9/14
to google-analyti...@googlegroups.com
You're generating the JS sourcecode that would need to be run in a browser. This could if your given link would be opend in an iframe for example on the thankyou page.
Check out the Measurement Protocoll to send Data to GA via direct server calls (no browser required)

Ankur Gandhi

unread,
Jan 9, 2014, 2:31:44 PM1/9/14
to google-analyti...@googlegroups.com
Hi Lukas,

Really Thanks for reply. This is what I wanted. I hope it will work for sure because browser not needed for this, But I have 1 more question that I just tried measurement protocol with temporary file and temp data by calling this link in browser : http://example.com/conv_test.php?subid=6b3fbf87a6a14e696bed1aadfc36fb8b&storename=Measurement12&orderid=MEA111&total=1.20

and it is generating this link : https://ssl.google-analytics.com/collect?payload_data&v=1&tid=UA-XXXXXXX-1&cid=234343454523.1389290688&t=transaction&ti=MEA111&ta=Measurement12&tr=1.19&cu=EUR

It added transaction in Google Analytics, but it is not show any data related to mobile, like mobile device name, Mobile branding in Google Analytics.

Here is my file content(just sending you needed code only) :

$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$result = wp_remote_get( $getString );

in $data all parameter passed. When I used analytics.js addTransaction then it was showing all info properly but for measurement protocol it is not showing mobile related data.I read I need to send user_agent_string but how I have to send user_agent_string? Should I send it by adding it to (https://ssl.google-analytics.com/collect) or $getString? or any other way.Next How I can send and most important thing how I get user agent by javascript function or php or something else?

Pls let me know.

Thanks in Advance.
Ankur

Lukas Grebe

unread,
Jan 10, 2014, 3:33:18 AM1/10/14
to google-analyti...@googlegroups.com
i dont think the measurement protocoll support a useragent string.

if it's important to you, you might try to get the relevant information into a custom dimension.

Ankur Gandhi

unread,
Jan 10, 2014, 6:24:21 AM1/10/14
to google-analyti...@googlegroups.com
Hi Lukas,

Thanks for reply.

https://developers.google.com/analytics/devguides/collection/protocol/v1/reference you can see here about user_agent_string

  • user_agent_string – Is a formatted user agent string that is used to compute the following dimensions: browser, platform, and mobile capabilities.

    If this value is not set, the data above will not be computed.

But important thing how to get user_agent_string and how to pass it with URL?


Thanks

Ankur

  • user_agent_string – Is a formatted user agent string that is used to compute the following dimensions: browser, platform, and mobile capabilities.

    If this value is not set, the data above will not be computed.

Reply all
Reply to author
Forward
0 new messages