Search code examples
javascriptphpe-commercegoogle-analytics-apigoogle-analytics-firebase

Converting variables from PHP to Javascript for Google Analytics e-Commerce Tracking code


I am trying to install Google Analytics e-Commerce Tracking on a website, and while I have it installed in the right place, I am unsure of how to use my PHP code to dynamically create e-Commerce Transactions. The example js code that Google provides looks like this:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_addTrans',
    '1234',           // transaction ID - required
    'Acme Clothing',  // affiliation or store name
    '11.99',          // total - required
    '1.29',           // tax
    '5',              // shipping
    'San Jose',       // city
    'California',     // state or province
    'USA'             // country
  ]);

   // add item might be called for every item in the shopping cart
   // where your ecommerce engine loops through each item in the cart and
   // prints out _addItem for each
  _gaq.push(['_addItem',
    '1234',           // transaction ID - required
    'DD44',           // SKU/code - required
    'T-Shirt',        // product name
    'Green Medium',   // category or variation
    '11.99',          // 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);
  })();

</script>

Let's say that my transaction ID is a PHP variable called $ID, total is a variable called $amount, etc.

How do I use these variables in my tracking code to dynamically send the correct information to Google?


Solution

  • If the Javascript is in your HTML output, you can just put the PHP echo right inline (assuming you are using .php files and not .html files:

    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
      _gaq.push(['_trackPageview']);
      _gaq.push(['_addTrans',
        <?php echo $ID; ?>, // transaction ID - required
        'Acme Clothing',    // affiliation or store name
        '11.99',            // total - required
        '1.29',             // tax
        '5',                // shipping
        'San Jose',         // city
        'California',       // state or province
        'USA'               // country
      ]);
    
       // add item might be called for every item in the shopping cart
       // where your ecommerce engine loops through each item in the cart and
       // prints out _addItem for each
      _gaq.push(['_addItem',
        <?php echo $ID; ?>, // transaction ID - required
        'DD44',             // SKU/code - required
        'T-Shirt',          // product name
        'Green Medium',     // category or variation
        '11.99',            // 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);
      })();
    
    </script>
    

    Or if your javascript is in a script file, you could write the javascript variable to the html page prior to loading the javascript file then use this new variable (in the example below transactionID):

    <script>
        var transactionID = <?php echo $ID; ?>;
    </script>
    
    <script src="js/myscript.js"></script>