Jump to content

Google Analytics (ecommerce) that works!


pod

Recommended Posts

After many tries at getting this to work on and off again part time over a year. We dove back into this and finally got the ecommerce portion of GA working. Hopefully WHMCS admins will update the wiki with this!

 

Here is what you need to do!

 

Place the following GA code in the header.tpl just before the </HEAD>.

 

 

{literal}
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
 _gaq.push(['_trackPageview']);

 (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>
{/literal}

 

 

Now goto the client\templates\orderforms\default\complete.tpl template and place the following code exactly as it is at the end of the template. NOTE: We placed this outside of the {if $ispaid}

 

 

 

{php}
$orderid = $this->get_template_vars('orderid');
$userid = $_SESSION["uid"];

// get client data
$result = select_query("tblclients","city, state, country",array("id"=>$userid));
$data = mysql_fetch_array($result);
$city = $data["city"];
$state = $data["state"];
$country = $data["country"];
$this->assign('city',$city);
$this->assign('state',$state);
$this->assign('country',$country);

// get package name
$result = select_query("tblhosting","packageid",array("orderid"=>$orderid));
$data = mysql_fetch_array($result);
$packageid = $data["packageid"];
$result = select_query("tblproducts","name, type",array("id"=>$packageid));
$data = mysql_fetch_array($result);
$name = $data["name"];
$type = $data["type"];
$this->assign('name',$name);
$this->assign('type',$type);
$this->assign('packageid',$packageid);
{/php}

<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-7069306-1']);
 _gaq.push(['_trackPageview']);
 _gaq.push(['_addTrans',
   '{$orderid}',	// order ID - required
   '',				// affiliation or store name
   '{$amount}',	// total - required
   '0',			// tax
   '0',			// shipping
   '{$city}',		// city
   '{$state}',		// state or province
   '{$country}'	// 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',
   '{$orderid}',	// order ID - required
   '{$packageid} - {$name}',	// SKU/code - required
   '{$name}',		// product name
   '{$type}',		// category or variation
   '{$amount}',	// unit price - required
   '1'				// quantity - required
 ]);
 _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

 _gaq.push(['_trackPageview', '/client/cart.php?a=complete&pid={$packageid}']);

{literal}
 (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);
 })();
{/literal}

</script>

 

 

There code above was provided by someone on the forums. I made two minor changes to the code above.

 

1) I added the product name to the SKU as i couldnt stand seeing the PID # without the name in the ecommerce section.

 

2) The other added line was: _gaq.push(['_trackPageview', '/client/cart.php?a=complete&pid={$packageid}']); - which was used to make the GOAL URLs unqiue. You dont need this line, but if using goal you will probably want this.

 

I will have another post about goals shortly.

 

 

FYI: This works with gateways that let you enter the CC info on the order form and auto-charge it. Not sure how something with paypal or other redirect gateways will work or not. We use authorize.net. BTW: I believe that regardless of the settings on General Settings - > Auto Redirect on Checkout I think all of them end up on the completed page now.

 

I hope this helps others out there!

Link to comment
Share on other sites

  • 3 weeks later...

The above is good and will work, but for ease of use you can also place the google analytics javascript into a external javascript files. two of them

 

lets say you have them here

 

public_html/js/google1.js

public_html/js/google2.js

 

Your whmcs installation is in say public_html/whmcs/

 

header.tpl will call up google1.js

 

footer.tpl will call up google2.js

 

On another note, if like me you use javascript for image swaps you can also place the relevance js into external files and call it the same way.

 

Im not a javascript coder infact i cant get my head around it properly so if i can do it you can too

Link to comment
Share on other sites

  • 6 months later...

I have tried the above code and can't get it working.

 

Placing some debug statements in the PHP it appears that there may be a PHP error at the select_query() call because:

- stuff echo'd immediately before it appears but immediately after does not

- non of the assignments to smarty variables succeed - they're all blank

 

Is there some way of finding out why the select_query() call fails? Any ideas as to the cause?

 

Thanks,

 

Mark

Link to comment
Share on other sites

Solved!

 

THE ABOVE CODE IS BROKEN AND MUST BE CHANGED TO MAKE IT WORK!

 

The problem is the "//" comments in the PHP section. They are illegal there, so if you remove "// get client data" and similar lines in the PHP section the code works. Note, the "//" comments in the rest of the code are fine, only the ones between {php} and {/php} need be removed (or I guess changed to '#').

 

Mark

Link to comment
Share on other sites

..you can also place the google analytics javascript into a external javascript files.

 

 

I have a lot of trouble with JS stuff in WHMCS. I can't really understand the relationship that the {literal} statement has with WHMCS and Smarty.

 

From what you are saying, its possible to call a js function without the confusion over whether to use a {literal] or not?

 

 

If like me you use javascript for image swaps you can also place the relevance js into external files and call it the same way.

 

I have been trying to get a decent image swap / slider for my site (any reccomendations anyone?) but can never get the js working in a WHMCS page. From what you say the solution could be the use of external JS files?

 

Trevor

Link to comment
Share on other sites

If in your java code there is a function it normally uses the curley braces { and }

Smarty interperates the code incorrectly as it expects smarty code within the curley braces and if smarty doesn't recognise it then it throws an error which generally also makes the java to also not work.

by placing the {literal}... {/literal} tags around that code smarty will not alter anything between the literal tags.

 

Another way to do it is to replace the braces in the jave to {ldelm} and {rdelm} respectfully so that smarty will replace the tags with the correct curley braces.

Link to comment
Share on other sites

That’s half how I thought it worked. :)

 

The literal tag is taken ‘literally’ and code within those tags is not interfered with during the execution by Smarty.

 

I wasn’t aware of the {ldelm} and {rdelm} tags so that is a useful tip.

 

I’ll try again and put the delimiters round my code and see if anything works. Fingers crossed :?

Link to comment
Share on other sites

I've already got an include in my footer.tpl file to bring my Google Analytics code in, which has the following:

 

<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
 _gaq.push(['_trackPageview']);

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

 

So I'd like to have another include file in my \templates\orderforms\default\complete.tpl template that has the code in this thread to track my purchases. But the code I can't just copy the code form here as it then doubles up, so I'd like to know what code I needs to use in the complete.tpl file that does not include the code that is in my footer.tpl file, if that's possible.

 

At the bottom of my footer.tpl file, I've got this code:

 

<!--Google Analytics Tracking Code-->
{php} include("../includes/analyticstracking-whmcs.php"); {/php}
</body>

 

So I'd like to have the correct code for the ecommerce tracking in an include file too, in the appropriate place in the complete.tpl file, like this:

 

<!--Google Analytics Tracking Code for Ecommerce-->
{php} include("../includes/analyticstracking-whmcs-ecommerce.php"); {/php}

 

But obviously I can't have the {php} {/php} there as that coding already exists in the code that would be in the include file.

 

Look forward to any assistance on this.

Link to comment
Share on other sites

Hi Jason,

 

I'm not sure what you need, as since I posted I've found that while I fixed one part of the problem (getting values for e-commerce tracking from WHMCS into the PHP generated code), the results are not showing up so my analytics code may be the problem.

 

It isn't urgent for me to look into this so don't wait for a solution from me as I have other priorities for the time being.

 

Good luck, and please keep this thread updated with anything you try or learn, especially if you get a solution.

 

Mark

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated