Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 16 of 16

Thread: Google Analytics Asynchronous Syntax for eCommerce Tracking

  1. #1
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Lightbulb Google Analytics Asynchronous Syntax for eCommerce Tracking

    For those wanting to move to Google Analytics' recommended Asynchronous Syntax for eCommerce Tracking, here's is an XML package you can use that you can reference in any template. To use the package, add it to your Skin XmlPackages folder and then reference it in the template just above the </body> closing tag with:

    Code:
    <aspdnsf:XmlPackage ID="analyticspackage" runat="server" PackageName="rev.analytics.xml.config" />
    The package adds order & item info to the Order Confirmation page as well:

    Code:
    <?xml version="1.0" standalone="yes" ?>
    <package version="2.1" displayname="Analytics" debug="false" includeentityhelper="false">
      <query name="Order" rowElementName="OrderInfo">
        <sql>
          <![CDATA[
          select o.*, (case when o.AffiliateID = 0
    then 'None' else (select a.Name from dbo.Affiliate a  with (NOLOCK) where a.AffiliateID = o.AffiliateID) end) as AffiliateName From dbo.orders o with (NOLOCK)
    where o.ordernumber = @ordernum
           ]]>
        </sql>
        <queryparam paramname="@ordernum" paramtype="request" requestparamname="ordernumber" defvalue="0" sqlDataType="int" validationpattern="^\d{1,9}$"/>
      </query>
      <query name="OrderItems" rowElementName="Item">
        <sql>
          <![CDATA[
              SELECT  s.ShoppingCartRecID, 
                      s.OrderNumber, 
                      s.ProductID, 
                      s.VariantID, 
                      s.Quantity, 
                      s.ChosenColor, 
                      s.ChosenSize, 
                      s.OrderedProductName, 
                      s.OrderedProductVariantName, 
                      s.OrderedProductSKU, 
                      s.OrderedProductPrice, 
                      s.OrderedProductRegularPrice, 
                      s.OrderedProductSalePrice, 
                      s.OrderedProductExtendedPrice, 
                      s.OrderedProductQuantityDiscountName, 
                      s.OrderedProductQuantityDiscountID, 
                      s.OrderedProductQuantityDiscountPercent, 
                      s.IsShipSeparately, 
                      s.IsDownload, 
                      s.FreeShipping, 
                      s.TextOption, 
                      s.ShippingMethod, 
                      s.Notes, 
                      s.ExtensionData, 
                      s.CustomerEntersPrice, 
                      s.GiftRegistryForCustomerID, 
                      s.ShippingAddressID, 
                      s.ShippingDetail, 
                      s.SizeOptionPrompt, 
                      s.ColorOptionPrompt, 
                      s.TextOptionPrompt, 
                      s.IsTaxable, 
                      s.TaxClassID, 
                      s.TaxRate,
                      ISNULL(s.IsAKit, 0) AS IsAKit, 
                      ISNULL(s.IsAPack, 0) AS IsAPack, 
                      ISNULL(s.IsSystem, 0) AS IsSystem
              FROM dbo.Orders_ShoppingCart s WITH (NOLOCK) 
              WHERE s.ordernumber = @ordernum
              ORDER by s.ShippingAddressID
                ]]>
        </sql>
        <queryparam paramname="@ordernum" paramtype="request" requestparamname="ordernumber" defvalue="0" sqlDataType="int" validationpattern="^\d{1,9}$"/>
      </query>
    
      <PackageTransform>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" exclude-result-prefixes="aspdnsf">
          <xsl:output method="html" omit-xml-declaration="yes"/>
          <xsl:variable name="OrderNumber" select="/root/Order/OrderInfo/OrderNumber" />
          <xsl:template match="/">
            <script type="text/javascript">
              var _gaq = _gaq || [];
              _gaq.push(['_setAccount', '<xsl:value-of select="aspdnsf:AppConfig('Google.AnalyticsAccount')" disable-output-escaping="yes"/>']);
              _gaq.push(['_trackPageview']);
              <xsl:if test="/root/Runtime/PageName = 'orderconfirmation.aspx'">
                _gaq.push(['_addTrans',
                '<xsl:value-of select="$OrderNumber" />',
                '<xsl:value-of select="/root/Order/OrderInfo/AffiliateName" />',
                '<xsl:value-of select="format-number(/root/Order/OrderInfo/OrderTotal, '0.00')" />',
                '<xsl:value-of select="format-number(/root/Order/OrderInfo/OrderTax, '0.00')" />',
                '<xsl:value-of select="format-number(/root/Order/OrderInfo/OrderShippingCosts, '0.00')" />',
                '<xsl:value-of select="/root/Order/OrderInfo/BillingCity" />',
                '<xsl:value-of select="/root/Order/OrderInfo/BillingState" />',
                '<xsl:value-of select="/root/Order/OrderInfo/BillingCountry" />'
                ]);
                <xsl:apply-templates select="/root/OrderItems/Item" />
                _gaq.push(['_trackTrans']);
              </xsl:if>
              (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>
          </xsl:template>
    
          <xsl:template match="Item">
            _gaq.push(['_addItem',
            '<xsl:value-of select="OrderNumber" />',
            '<xsl:value-of select="OrderedProductSKU" />',
            '<xsl:value-of select="OrderedProductName" />',
            '',   // category or variation - could add color or size here
            '<xsl:value-of select="format-number(OrderedProductPrice div Quantity, '0.00')" />',
            '<xsl:value-of select="Quantity" />'
            ]);
          </xsl:template>
        </xsl:stylesheet>
      </PackageTransform>
    </package>
    Last edited by don.irwin; 12-08-2011 at 08:22 AM. Reason: added empty field for item category or variation

  2. #2
    medsupply is offline Senior Member
    Join Date
    Jul 2011
    Posts
    99

    Default

    Don,

    My analytics is not showing the conversions, even when I followed the ASPDNSF manual to a T. Therefore I decided to try your solution.

    I tried it but the website gives me a syntax error in the code that inserts the package in the template. I am using the 9.2 store.

    Parser Error Message: Unknown server tag 'aspdnsf:XmlPackage'.

    Any idea why this happens?

  3. #3
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    simple - the tag needs to be registered. Add this to your template masters:

    Code:
    <%@ Register TagPrefix="aspdnsf" TagName="XmlPackage" Src="~/Controls/XmlPackageControl.ascx" %>

  4. #4
    medsupply is offline Senior Member
    Join Date
    Jul 2011
    Posts
    99

    Default

    After I implemented you fix, it did not give me any more errors but I still have no conversions in my analytics after 36 hours.

    Is there anything that needs to be done in the AppConfigs?

  5. #5
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    make sure you have the app.config value for Google.AnalyticsAccount set to your GA account. Also, do a view source on the pages and make sure the values are populating correctly.

  6. #6
    medsupply is offline Senior Member
    Join Date
    Jul 2011
    Posts
    99

    Default

    I had an incorrect value in the analytics account AppConfig. After fixing it I saw a conversion in my analytics for the 1st time. But it is counting double conversions.

    If using your tracking xml package, do I need to turn off any of the App.Configs?

    Google.EcomOrderTrackingEnabled, IncludeGoogleTrackingCode (I have shareasale tracking in this topic)

    Thanks Don!
    PS: I read your post about having 2 sites on the same Rackspace cloud server and the effect on SEO. Can you please send me a private message? I need your services making sure our server is configured properly before going live.

  7. #7
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    I have both those values set to "true" but I am not using them in the template or the XML package, so they shouldn't really matter. Make sure the template being used by orderconfirmation.aspx.cs doesn't insert the 2nd set of values/tracking. I add the folowing to orderconfirmation.aspx.cs to make sure the template I want to use is being called:

    Code:
            protected override string OverrideTemplate()
            {
                return "carttemplate.master";
            }

  8. #8
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    try setting Google.EcomOrderTrackingEnabled to "false" - I see there is logic in orderconfirmation.aspx.cs to append code if enabled. I tend to override a lot of this logic an sometimes forget what I changes :-(

  9. #9
    arathi@expointl.com is offline Junior Member
    Join Date
    Apr 2011
    Posts
    1

    Default Same issue as previous

    I implemented as stated above but it does not work and i can't see anything while I view the source.

  10. #10
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    how exactly did you implement?

    Quote Originally Posted by arathi@expointl.com View Post
    I implemented as stated above but it does not work and i can't see anything while I view the source.

  11. #11
    usascholar is offline Member
    Join Date
    Jan 2010
    Posts
    64

    Default

    Hi Don,

    I am trying to modify your code a bit for the google trusted stores Xml package.
    Can you take a look at it here?

    http://forums.aspdotnetstorefront.co...e-Can-you-help

    thanks for any help on this!
    A
    Last edited by usascholar; 06-13-2012 at 07:19 AM.

  12. #12
    Skriver is offline Senior Member
    Join Date
    Apr 2012
    Posts
    188

    Default

    I have implemented the code and its working but like the other poster we are getting duplicate transactions showing.

    When I try to add the code below I get an error on orderconfirmation.aspx even though the order goes thorugh. It says it is missing the expected ;

    can you please post exactly as you have it in your orderconfirmation.aspx file so I can compare as I'm not sure where best to put this code and have the page work.

    protected override string OverrideTemplate()
    {
    return "carttemplate.master";
    }

  13. #13
    Skriver is offline Senior Member
    Join Date
    Apr 2012
    Posts
    188

    Default

    Its ok I got it in the end.

  14. #14
    Skriver is offline Senior Member
    Join Date
    Apr 2012
    Posts
    188

    Default

    How would we pass the Product Category as part of the order confirmation

    It currently works fine but it would be perfect to include Category as well as this is part of the E-Commerce tracking in Google.

    Anybody know what the xsl:value-of select is for the Product category?

  15. #15
    Skriver is offline Senior Member
    Join Date
    Apr 2012
    Posts
    188

    Default

    Hi

    The code is working great but under transactions revenue each product is being doubled. Product revenue is fine but transaction revenue its being counted twice. I have followed all the steps above but wondered if you knew what could be causing this.

    Thanks

  16. #16
    Tom Cox is offline Senior Member
    Join Date
    Nov 2009
    Location
    Los Angeles, CA
    Posts
    104

    Default

    Don,

    Thank you very much for this code. It's just what I need. Can you answer a dumb question? Where does the following line go?

    Code:
    <aspdnsf:XmlPackage ID="analyticspackage" runat="server" PackageName="rev.analytics.xml.config" />
    I understand the rest but I'm not sure what you mean by the "template" file.

    Thanks,
    Tom