railslove / rack-tracker

Tracking made easy: Don’t fool around with adding tracking and analytics partials to your app and concentrate on the things that matter.
https://www.railslove.com/open-source
MIT License
647 stars 121 forks source link

Google Global integration: purchase transaction_id #137

Closed marisveide closed 4 years ago

marisveide commented 5 years ago

In Google Global - to track the purchase event, the required field is transaction_id. At the moment, if we send only the value for the purchase, it's actually not showing up in Google Analytics.

It doesn't work if we send the following request (it's how currently rack-tracker is sending the purchase event):

gtag('event', 'purchase', {
 "value": 50,
});

It only shows up in Google Analytics if we send the request like this:

gtag('event', 'purchase', {
 "transaction_id": "1",
 "value": 50,
 "currency": "USD"
});

What should I do to send it with the all required attributes?

Ideally, even with attributes like this:

gtag('event', 'purchase', {
"event_label": "Charge 50",
 "transaction_id": "1",
 "value": 50,
 "currency": "USD"
});

Thanks so much!

Maris

bumi commented 5 years ago

Hi Maris,

how do you call rack-tracker? how does your controller code look like? I am not super familar with google global, but the rack tracker google global template should be pretty flexible: https://github.com/railslove/rack-tracker/blob/master/lib/rack/tracker/google_global/template/google_global.erb#L20

marisveide commented 5 years ago

Hi, @bumi !

Thank you for the reply - this gives hope that I am just not using it correctly.

Here's the sample code in my controller:

  def gg_purchase
    gg_options = {}
    gg_options[:action] = 'purchase'
    gg_options[:value] = 1
    gg_options[:currency] = 'USD'
    gg_options[:transaction_id] = 1
    gg_options[:label] = 'Basic'
    gg_options[:quantity] = 1
    gg_options[:items] = [{ "name": 'Basic', "quantity": 1, "price": 1 }]

    data = {
      type: 'event',
    }.merge(gg_options)

    if tracker { |t|
      t.google_global :event, data
    }
    end
  end

And as a result, it generates the following JavaScript:

<script asyncsrc='https://www.googletagmanager.com/gtag/js?id=UA-98918029-1'></script>
<script>
 window.dataLayer = window.dataLayer \|\| [];
 function gtag(){dataLayer.push(arguments)};
 gtag('js', new Date());
    
 gtag('config', 'UA-98918029-1', {});
 gtag('config', 'UA-98918029-3', {});
 gtag('config', 'UA-98918029-4', {});
 gtag('config', 'UA-102016273-1', {});
 gtag('config', 'UA-102016273-2', {});

 gtag('event', 'purchase', {"event_label":"Basic","value":1});
</script>

As you can see, not all options are passed there. And this Purchase event, therefore, is not appearing in Google Analytics, because the transaction_id is definitely mandatory.

My initializers/rack_tracker.rb file:

if Rails.env.production? || true
  Rails.application.config.middleware.use(Rack::Tracker) do
    handler :google_global, {
      trackers: [
        { id: 'UA-98918029-1' },
        { id: 'UA-98918029-3' }
        { id: 'UA-98918029-4' },
        { id: 'UA-102016273-1' },
        { id: 'UA-102016273-2' },
      ]
    }
end

Tracker source code:

As far as I found, here's the place which actually accepts only the category, label and value params, and filters everything else off: https://github.com/railslove/rack-tracker/blob/master/lib/rack/tracker/google_global/google_global.rb#L18

Thanks so much for your help!

bumi commented 5 years ago

ok, yes, looks good. I think (that if is probably not needed?)

and yeah, you are right. parameters are filtered. Do you want to create a PR adding more possible parameters to that list?

Also have a look at the conversation in the PR adding this feature: https://github.com/railslove/rack-tracker/pull/126

marisveide commented 5 years ago

Thanks, @bumi !

OK, I am ready to make the proposed change - and the direction I am thinking is to remove the filtering of those attributes completely. To pass to gtag all the attributes which were provided from the options. That is, why duplicate this specification in Ruby code, where for example, for Enhanced ecommerce you can pass all those attributes: https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce

Do you have any reason why those need to be filtered at all?

bumi commented 5 years ago

@marisveide nice, that would be awesome.

I honestly don't know the details about gtag events, maybe @atd (who developed that feature) remembers why those filters were added.

atd commented 5 years ago

Hi @bumi @marisveide

I added them to reproduce the existing behavior in the google_analytics tracker https://github.com/railslove/rack-tracker/blob/master/lib/rack/tracker/google_analytics/google_analytics.rb#L20

But I agree, I think it would be more convenient to drop them and gain in flexibility

marisveide commented 5 years ago

Alright, change made, PR made, checks passed (except for one, which looks like build config issue)... You == merge? ;)