CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Integrating Google Analytics ecommerce tracking code
Pages: [1]

Author Topic: Integrating Google Analytics ecommerce tracking code  (Read 2762 times)

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Integrating Google Analytics ecommerce tracking code
March 14, 2011, 10:37:13 am
Here's a patch for LineItem.tpl that integrates the Google Analytics ecommerce tracking code.  This works for events that use price sets.  If you don't use price sets, then you'll probably need to modify ThankYou.tpl instead.  Recommend copying the appropriate template into a custom template directory rather than modifying the original -- that way the changes will persist across upgrades.

Since LineItem.tpl gets called multiple times during the event registration process, we check $trxn_id so the tracking code only gets inserted at the final step.

This code uses the price set field names as the item names/SKUs, and the event name as the category/variation.  You will need to insert your GA account number in the getTracker function, and your org name/location in the addTrans function.

Code: [Select]
--- templates/CRM/Price/Page/LineItem.tpl.orig 2011-02-27 11:12:36.000000000 -0800
+++ templates/CRM/Price/Page/LineItem.tpl 2011-03-14 09:29:19.000000000 -0700
@@ -24,6 +24,32 @@
  +--------------------------------------------------------------------+
 *}
 {* Displays contribution/event fees when price set is used. *}
+{if $trxn_id}
+    {literal}
+    <script type="text/javascript">
+        var gaJsHost = (("https:" == document.location.protocol ) ? "https://ssl." : "http://www.");
+        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
+    </script>
+    <script type="text/javascript">
+        try{
+            var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
+            pageTracker._trackPageview();
+            pageTracker._addTrans(
+                {/literal}
+                "{$trxn_id}",                       // Order ID
+                "My Organization Name",             // Org name
+                "{$totalAmount}",                   // Order total
+                {literal}
+                "0",                                // tax
+                "0",                                // shipping
+                "Somecity",                         // city
+                "California",                       // state
+                "USA"                               // country
+            );
+        } catch(err) {}
+    </script>
+    {/literal}
+{/if}
 {foreach from=$lineItem item=value key=priceset}
     {if $value neq 'skip'}
     {if $lineItem|@count GT 1} {* Header for multi participant registration cases. *}
@@ -46,6 +72,24 @@
                 <td class="right">{$line.line_total|crmMoney}</td>
          {if $pricesetFieldsCount}<td class="right">{$line.participant_count}</td> {/if}
             </tr>
+            {if $trxn_id}
+                {literal}
+                <script type="text/javascript">
+                    try {
+                        pageTracker._addItem(
+                            {/literal}
+                            "{$trxn_id}",           // Order ID
+                            "{$line.label}",        // SKU/code
+                            "{$line.label}",        // Product name
+                            "{$event.event_title}", // Category or variation
+                            "{$line.unit_price}",   // Unit price
+                            "{$line.qty}"           // Quantity
+                            {literal}
+                        );
+                    } catch(err) {}
+                </script>
+                {/literal}
+            {/if}
             {/foreach}
     </table>
     {/if}
@@ -81,6 +125,16 @@
      </div>   
 </div>
 
+{if $trxn_id}
+    {literal}
+    <script type="text/javascript">
+        try {
+            pageTracker._trackTrans();
+        } catch(err) {}
+    </script>
+    {/literal}
+{/if}
+
 {if $hookDiscount.message}
     <div class="crm-section hookDiscount-section">
         <em>({$hookDiscount.message})</em>

cognoscento

  • I post occasionally
  • **
  • Posts: 44
  • Karma: 1
  • CiviCRM version: 4.2
  • CMS version: Drupal 7.17
  • MySQL version: 5.1.65
  • PHP version: 5.3.17
Re: Integrating Google Analytics ecommerce tracking code
November 25, 2012, 10:57:57 am
Will this patch still work with the newest version of CiviCRM?

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Re: Integrating Google Analytics ecommerce tracking code
November 25, 2012, 08:24:00 pm
I just tried it against CiviCRM 4.2.6.  The patch utility needed to apply some line offsets, but it still worked.

magnolia61

  • I post occasionally
  • **
  • Posts: 37
  • Karma: 0
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34 / Joomla 3.3.6
  • MySQL version: MySQL 5.5.40
  • PHP version: PHP 5.5.19
Re: Integrating Google Analytics ecommerce tracking code
December 03, 2012, 06:04:05 am
Hi  jcm55,
Can you post the syntax you used?
Did you use the traditional or the newer asynchronous Google Analytics code?
Thanks, Richard

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Re: Integrating Google Analytics ecommerce tracking code
December 04, 2012, 10:57:27 am
An already-patched copy of LineItem.tpl for CiviCRM 4.2 is below.  This does use the newer async code.  You should put this in your civicrm custom templates directory as CRM/Price/Page/LineItem.tpl.  Don't forget to change the GA account ID, org name, city and state.

Code: [Select]
{*
 +--------------------------------------------------------------------+
 | CiviCRM version 4.2                                                |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2012                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*}

{* Displays contribution/event fees when price set is used. *}
{if $trxn_id}
    {literal}
    <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol ) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
        try{
            var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
            pageTracker._trackPageview();
            pageTracker._addTrans(
                {/literal}
                "{$trxn_id}",                       // Order ID
                "My Organization Name",             // Org name
                "{$totalAmount}",                   // Order total
                {literal}
                "0",                                // tax
                "0",                                // shipping
                "Somecity",                         // city
                "California",                       // state
                "USA"                               // country
            );
        } catch(err) {}
    </script>
    {/literal}
{/if}
{foreach from=$lineItem item=value key=priceset}
    {if $value neq 'skip'}
    {if $lineItem|@count GT 1} {* Header for multi participant registration cases. *}
        {if $priceset GT 0}<br />{/if}
        <strong>{ts}Participant {$priceset+1}{/ts}</strong> {$part.$priceset.info}
    {/if}
    <table>
            <tr class="columnheader">
    <th>{ts}Item{/ts}</th>
    {if $context EQ "Membership"}
    <th class="right">{ts}Fee{/ts}</th>
                {else}
    <th class="right">{ts}Qty{/ts}</th>
                    <th class="right">{ts}Unit Price{/ts}</th>
    <th class="right">{ts}Total Price{/ts}</th>
{/if}
               
{if $pricesetFieldsCount}
    <th class="right">{ts}Total Participants{/ts}</th>{/if}
            </tr>
            {foreach from=$value item=line}
            <tr>
                <td>{if $line.html_type eq 'Text'}{$line.label}{else}{$line.field_title} - {$line.label}{/if} {if $line.description}<div class="description">{$line.description}</div>{/if}</td>
{if $context NEQ "Membership"}
    <td class="right">{$line.qty}</td>
                    <td class="right">{$line.unit_price|crmMoney}</td>
{/if}
                <td class="right">{$line.line_total|crmMoney}</td>
          {if $pricesetFieldsCount}<td class="right">{$line.participant_count}</td> {/if}
            </tr>
            {if $trxn_id}
                {literal}
                <script type="text/javascript">
                    try {
                        pageTracker._addItem(
                            {/literal}
                            "{$trxn_id}",           // Order ID
                            "{$line.label}",        // SKU/code
                            "{$line.label}",        // Product name
                            "{$event.event_title}", // Category or variation
                            "{$line.unit_price}",   // Unit price
                            "{$line.qty}"           // Quantity
                            {literal}
                        );
                    } catch(err) {}
                </script>
                {/literal}
            {/if}
            {/foreach}
    </table>
    {/if}
{/foreach}

<div class="crm-section no-label total_amount-section">
    <div class="content bold">
        {if $context EQ "Contribution"}
            {ts}Contribution Total{/ts}:
        {elseif $context EQ "Event"}
            {ts}Event Total{/ts}:
  {elseif $context EQ "Membership"}
            {ts}Membership Fee Total{/ts}:
        {else}
            {ts}Total Amount{/ts}:
        {/if}
    {$totalAmount|crmMoney}
    </div>
    <div class="content bold">
      {if $pricesetFieldsCount}
      {ts}Total Participants{/ts}:
      {foreach from=$lineItem item=pcount}
        {if $pcount neq 'skip'}
        {assign var="lineItemCount" value=0}

        {foreach from=$pcount item=p_count}
          {assign var="lineItemCount" value=$lineItemCount+$p_count.participant_count}
        {/foreach}
        {if $lineItemCount < 1 }
        {assign var="lineItemCount" value=1}
        {/if}
        {assign var="totalcount" value=$totalcount+$lineItemCount}
        {/if}
      {/foreach}
      {$totalcount}
      {/if}
     </div>   
</div>

{if $trxn_id}
    {literal}
    <script type="text/javascript">
        try {
            pageTracker._trackTrans();
        } catch(err) {}
    </script>
    {/literal}
{/if}

{if $hookDiscount.message}
    <div class="crm-section hookDiscount-section">
        <em>({$hookDiscount.message})</em>
    </div>
{/if}

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Integrating Google Analytics ecommerce tracking code

This forum was archived on 2017-11-26.