Using Generic Writer to load events to Google Analytics

ย 

I've already written an article introducing Generic Writer. And since today (or yesterday - it will be probably after midnight in Prague when this post is finished) I have a new use case for this component.

We don't want to force Keboola Connection to random people who don't care. We rather use our data to better target the audience. After initial struggles, we've managed to set up the Writer in a way it loads new events to our GA via Measurement Protocol.

Let's use an event of someone creating their first extractor in Pay As You Go project. The API call is pretty simple and can look like this:

curl -X "POST" "http://www.google-analytics.com/collect?v=1&t=event&tid=GA-PROPERTY-ID&cid=2139584944.1617277569&ni=false&ec=payg&ea=payg-extractor&ev=10&cu=USD&el=0031t00000E8nMyAAJ"

You can find each parameter description in the GA documentation linked above.

And here is a Generic Writer configuration which can make such API call. It uses a bit new feature - EMPTY_REQUEST mode:

{
  "json_data_config": {
    "chunk_size": 1,
    "infer_types_for_unknown": true,
    "delimiter": "__",
    "column_types": []
  },
  "method": "POST",
  "mode": "EMPTY_REQUEST",
  "path": "http://www.google-analytics.com/collect?{{protocol_version}}&{{hit_type}}&{{tracking_id}}&{{client_id}}&{{non_interaction_event}}&{{event_category}}&{{event_action}}&{{event_label}}&{{event_value}}&{{currency}}&{{queue_time}}",
  "iteration_mode": {
    "iteration_par_columns": [
      "protocol_version",
      "hit_type",
      "tracking_id",
      "client_id",
      "non_interaction_event",
      "event_category",
      "event_action",
      "event_label",
      "event_value",
      "currency",
      "queue_time"
    ]
  },
  "headers": [
    {
      "key": "User-Agent",
      "value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
    }
  ],
  "debug": true,
  "additional_requests_pars": [],
  "user_parameters": {
    "value": ""
  }
}

EMPTY_REQUEST mode ignores the body. When we are sending new events via Measurement Protocol, the only thing that matters is bunch of parameters. In combination with iteration_mode, you can quite easily fill the parameters from the table prepared in previous transformation. I'm attaching part of the table for a reference:

The field values are then used instead of the placeholders in the API call, which results in the API call mentioned at the beginning.

And that's it. Nothing complicated.

I'd like to just quickly mention the User-Agent part. That was actually the only problem stopping us from successfully importing the events from Keboola Connection. It seems that User-Agent used by default has not been accepting by the Google Analytics API, so we've added some general one found on the internet and voila, problem solved. Thanks David Esner (who's constantly making the Generic Writer a better tool, btw) for coming up with that idea.

1