[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

trying to get javascript variables into Google Analytics code


I'm trying to get a few variables into a bit of Google Analytics code but not sure if I'm doing it right.

I want the var genOrderID I have assigned to be passed as the 1st param in addTrans and addItem, does the code below seem ok or should it be in '' 's if so wont it just be supplied as text?

Is there any way I can actually see the generated JS code, ive tried firefor generated source but it just shows as below?


<script type="text/javascript">

var genOrderID = new Date().getTime();

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-1']);

'_addTrans', genOrderID, // order ID - required     
'', // affiliation or store name     
'100.00', // total - required     
'', // tax     
'', // shipping     
'', // city     
'', // state or province     
''  // country   

'_addItem', genOrderID, // order ID - required     
'CTX', // SKUcode - required     
'Council Tax', // product name     
'', // category or variation     
'100.00', // unit price - required     
'1' // quantity - required   

_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

(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);


Open in new window

Neil Thompson
Neil Thompson
  • 3
  • 2
1 Solution
Chris StanyonCommented:
Yeah, the arguments are all string types. Cast it to string when you set it.

var genOrderID = String(new Date().getTime());
Neil ThompsonSenior Systems DeveloperAuthor Commented:
excellent thanks, I guess you dont know how to also drip a Java Form Bean in there do you? I've got one in format

<bean:write name="payCTXForm" property="paymentResponse.paymentReference"/>
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Superb, many thanks
Chris StanyonCommented:
No idea on the Java stuff, sorry!
Neil ThompsonSenior Systems DeveloperAuthor Commented:
:). I tried just adding what I put above and it worked instantly

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now