peaksandpies / universal-analytics

A node module for Google's Universal Analytics and Measurement Protocol
962 stars 146 forks source link

Enhanced ecommerce #25

Closed dominykas closed 9 years ago

dominykas commented 9 years ago

Starting a discussion sooner, rather than later... We (https://github.com/insidewarehouse) need to have enhanced ecommerce support and so we're going to build it, but I'd also like some input/agreement before we make a PR :)

Google's documentation:

For the most of it, I think we might be able to just copy the ecommerce.js API, i.e. activities and impressions are usually trackable only with event and pageview hit types, so I think it's OK to just create addProduct, addImpression and setAction as methods. Discussion topics:

The bigger problem is the transaction type. Documentation says that one should not be sending both - "regular" ecommerce and "enhanced" ecommerce requests (see "Important" section under "Overview"). Enhanced ecommerce also requires an explicit opt-in via admin/settings of the analytics property. Under the "regular" approach, multiple hits with item and one with transaction is sent. Under the enhanced - transaction data can be sent with any hit type, but it can also include "Product data" and "Action data" (with action type purchase I presume). The params for transaction data are the same, though. To summarize:

The two approaches are mutually exclusive. Discussion topics:

I'll see if I can find out what other libraries in other languages do, but for now - just opening up the discussion.

dominykas commented 9 years ago

One more alternative would be to keep universal-analytics as a "raw", "close to the metal" library and send all the relevant ecommerce params via options object (use standard validation logic, with extensions in a similar way to custom dimensions implementation) and build a new library to construct these params...

alessbell commented 9 years ago

@dominykas Hi Dominykas! We (https://github.com/breather) need enhanced ecommerce support too. Let me know how I can help!

dominykas commented 9 years ago

PR #29 is ready - it updates the acceptable params and also the validations, however it must be noted, that the lib DOES support all the EE params, except that without my PR it will give warnings if debug flag is set to true.

I also published a gampee module to help with constructing EE params (I'll update and document it further), but here's some sample code how to use it:

var gampee = require("gampee"),
    ga = require("universal-analytics"),
    _ = require("lodash");

var ecommerceParams = gampee([
    {
        // visitor saw two products in search
        "type": "impression",
        "list": "search",
        "products": [
            { "id": "shirtM", "name": "Nice T-Shirt (M)", "position": 1 },
            { "id": "shirtXL", "name": "Nice T-Shirt (XL)", "position": 2 }
        ],
        "currency": "EUR"
    },
    {
        // visitor saw one product in highlights
        "type": "impression",
        "list": "highlights",
        "products": [
            { "id": "pants", "name": "Pretty Pants" }
        ],
        "currency": "EUR"
    },
    {
        // and added one 5 of shirtM to cart
        "type": "add",
        "products": [
            { "id": "shirtM", "name": "Nice T-Shirt (M)", "position": 1, "quantity": 5 }
        ],
        "currency": "EUR"
    }
]);

var ua = ga("UA-00000000-0", "5bbb81ff-0757-44e0-8fcb-f263d982b95a", { debug: true });
ua.pageview(_.merge({
    dp: "/",
    cd20: "one",
    cm20: "two"
}, ecommerceParams));

Result:

[universal-analytics] Enqueued pageview ({"dp":"/","cd20":"one","cm20":"two","il1nm":"search","cu":"EUR","il1pi1id":"shirtM","il1pi1nm":"Nice T-Shirt (M)","il1pi1ps":"1","il1pi2id":"shirtXL","il1pi2nm":"Nice T-Shirt (XL)","il1pi2ps":"2","il2nm":"highlights","il2pi1id":"pants","il2pi1nm":"Pretty Pants","pa":"add","pal":"search","pr1id":"shirtM","pr1nm":"Nice T-Shirt (M)","pr1ps":"1","pr1qt":"5","v":"1","tid":"UA-00000000-0","cid":"5bbb81ff-0757-44e0-8fcb-f263d982b95a","t":"pageview"})
amit034 commented 9 years ago

tried this sample code got : [universal-analytics] Warning! Unsupported tracking parameter il1nm (search) [universal-analytics] Warning! Unsupported tracking parameter il1pi1id (shirtM) [universal-analytics] Warning! Unsupported tracking parameter il1pi1nm (Nice T-Shirt (M)) [universal-analytics] Warning! Unsupported tracking parameter il1pi1ps (1) [universal-analytics] Warning! Unsupported tracking parameter il1pi2id (shirtXL) [universal-analytics] Warning! Unsupported tracking parameter il1pi2nm (Nice T-Shirt (XL)) [universal-analytics] Warning! Unsupported tracking parameter il1pi2ps (2) [universal-analytics] Enqueued pageview ({"dp":"/search?q=some+product","cd20":"one","cm20":"two","il1nm":"search","cu":"EUR","il1pi1id":"shirtM","il1pi1nm":"Nice T-Shirt (M)","il1pi1ps":"1","il1pi2id":"shirtXL","il1pi2nm":"Nice T-Shirt (XL)","il1pi2ps":"2","v":"1","tid":"UA-62992924-1","cid":"5bbb81ff-0757-44e0-8fcb-f263d982b95a","t":"pageview"})

and no impression under conversation -> ecommerce -> overview

dominykas commented 9 years ago

The warnings are because the PR is still not merged.

As for impressions - just to double check - you know there's a delay until the stats display, right? Did the "add to cart" appear?