/    Sign up×
Community /Pin to ProfileBookmark

Random number inside a script

Hey
I need to generate random number for a specific script

the script is google analytics eCommerce script
I am using Google tag manager to implant the script using custom html

I need the id of the transaction to be same for both parts marked in red
first thing I did was to generate a random number using a macro in GTM
problem is i get different numbers for each transaction
what i now thought of doing is

var randomnumber=Math.floor(Math.random()*11)
randomnumber

but again I am getting different numbers

Question is how can I generate a random number that will be the same for both id’s

Thank you for your time and help.

<script>
ga(‘require’, ‘ecommerce’);
ga(‘ecommerce:addTransaction’, {
[COLOR=”#FF0000″]‘id’: ‘{{trans id}}’, // Transaction ID. Required.[/COLOR]
‘affiliation’: ‘category macro’, // Affiliation or store name.
‘revenue’: ‘3’, // Grand Total.
‘shipping’: ”, // Shipping.
‘tax’: ” // Tax.
});
ga(‘ecommerce:addItem’, {
[COLOR=”#FF0000″]‘id’: ‘{{trans id}}’, // Transaction ID. Required.[/COLOR]
‘name’: ‘Fluffy Pink Bunnies’, // Product name. Required.
‘sku’: ‘DD23444’, // SKU/code.
‘category’: ‘Party Toys’, // Category or variation.
‘price’: ‘11.99’, // Unit price.
‘quantity’: ‘1’ // Quantity.
});
ga(‘ecommerce:send’);

</script>

Thanks in advance

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootMar 02.2015 — You use a random number for what purpose?

If you are going to use a number thats random, store it in a variable to reuse the value again.

Have you tried something like this?

&lt;script&gt;
rand = Math.floor( Math.random() * 11 );

ga('require', 'ecommerce');

ga('ecommerce:addTransaction', {
'id':rand, // Transaction ID. Required.
'affiliation': 'category macro', // Affiliation or store name.
'revenue': '3', // Grand Total.
'shipping': '', // Shipping.
'tax': '' // Tax.
} );

ga('ecommerce:addItem', {
'id': rand, // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
} );

ga('ecommerce:send');
&lt;/script&gt;


If I understand your needs correctly.
Copy linkTweet thisAlerts:
@MiGHTyDuCKauthorMar 04.2015 — You use a random number for what purpose?

If you are going to use a number thats random, store it in a variable to reuse the value again.

Have you tried something like this?

&lt;script&gt;
rand = Math.floor( Math.random() * 11 );

ga('require', 'ecommerce');

ga('ecommerce:addTransaction', {
'id':rand, // Transaction ID. Required.
'affiliation': 'category macro', // Affiliation or store name.
'revenue': '3', // Grand Total.
'shipping': '', // Shipping.
'tax': '' // Tax.
} );

ga('ecommerce:addItem', {
'id': rand, // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
} );

ga('ecommerce:send');
&lt;/script&gt;


If I understand your needs correctly.[/QUOTE]


Thanks !

I created it as global variable and reused it
Copy linkTweet thisAlerts:
@rootMar 04.2015 — I still don't understand why you need a TxID thats random.

If you were to use the current time in milliseconds + the users email address piped in to an MD5 function, your resulting MD5 hash would be unique.

Alternatively if your on a server running PHP serverside, you could get the server to issue a TxID thats unique and have that written in to your script directly, if it needs to be a number then use a number based option.

<i>
</i>&lt;script&gt;
&lt;?php
$transactionID = uniqid("tx_",true);
echo "rand = '$transactionID';";
?&gt;
ga('require', 'ecommerce');

ga('ecommerce:addTransaction', {
'id':rand, // Transaction ID. Required.
'affiliation': 'category macro', // Affiliation or store name.
'revenue': '3', // Grand Total.
'shipping': '', // Shipping.
'tax': '' // Tax.
} );

ga('ecommerce:addItem', {
'id': rand, // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
} );

ga('ecommerce:send');
&lt;/script&gt;
×

Success!

Help @MiGHTyDuCK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...