Skip to content

Google Analytics plugin for sending events to Snowplow

Notifications You must be signed in to change notification settings

snowplow-incubator/snowplow-google-analytics-plugin

Repository files navigation

Google Analytics plugin for Snowplow

Build Status License

Overview

sp-ga-plugin.js lets you fork the payloads sent to Google Analytics to your Snowplow endpoint.

Technical Docs Setup Guide Roadmap & contributing
i1 i2 i3

Quickstart

You can use the plugin by requiring it and specifying your Snowplow endpoint:

<script>
  // usual isogram
  ga('create', 'UA-XXXXX-Y', 'auto');
  ga('require', 'spGaPlugin', { endpoint: 'https://my-collector.cloudfront.net' });
  ga('send', 'pageView');
</script>
<script async src="https://cdn.jsdelivr.net/gh/snowplow-incubator/snowplow-google-analytics-plugin@0.1.0/sp-ga-plugin.js"></script>

Deployment with Google Tag Manager

Google Tag Manager does not currently support loading plugins when using Google Analytics tag templates. A common workaround is to use a Custom HTML tag to load the tracker with the plugin, but this has the unfortunate consequence of requiring that all tags to which the plugin should be applied use the same tracker name. This is difficult to do with Google Tag Manager in a way that doesn't compromise data collection quality.

The best way to deploy this using Google Tag Manager is to replicate the plugin functionality by overwriting the relevant task in the GA hit builder task queue. But instead of modifying sendHitTask directly, a safer way is to approach it via customTask.

1. Create a new Custom JavaScript variable

Create a new Custom JavaScript variable, and name it {{customTask - Snowplow duplicator}}. Add the following code within:

function() {
  // Add your snowplow collector endpoint here
  var endpoint = 'https://my.snowplow.collector.com/';
  
  return function(model) {
    var vendor = 'com.google.analytics';
    var version = 'v1';
    var path = ((endpoint.substr(-1) !== '/') ? endpoint + '/' : endpoint) + vendor + '/' + version;
    
    var globalSendTaskName = '_' + model.get('trackingId') + '_sendHitTask';
    
    var originalSendHitTask = window[globalSendTaskName] = window[globalSendTaskName] || model.get('sendHitTask');
    
    model.set('sendHitTask', function(sendModel) {
      var payload = sendModel.get('hitPayload');
      originalSendHitTask(sendModel);
      var request = new XMLHttpRequest();
      request.open('POST', path, true);
      request.setRequestHeader('Content-type', 'text/plain; charset=UTF-8');
      request.send(payload);
    });
  };
}

This stores a reference to the original sendHitTask in a globally scoped variable (e.g. window['_UA-12345-1_sendHitTask']) to avoid multiple runs of this customTask from cascading on each other.

2. Add {{customTask - Snowplow duplicator}} to Google Analytics tags

This variable must be added to every single Google Analytics tag in the GTM container, whose hits you want to duplicate to Snowplow.

The best way to do this is to leverage the Google Analytics Settings variable.

Regardless of whether you choose to add this variable directly to the tags' settings or into a Google Analytics Settings variable, you need to do the following.

  1. Browse to the tags' More Settings option, expand it, and then expand Fields to set. If you are editing the tag directly (i.e. not using a Google Analytics Settings variable), you will need to check "Enable overriding settings in this tag" first.

  2. Add a new field with:

    • Field name: customTask
    • Value: {{customTask - Snowplow duplicator}}

All tags which have this field set will now send the Google Analytics payload to the Snowplow endpoint.

Further reading on the topic:

Questions or need help?

Check out the Talk to us page on our wiki.

Copyright and license

Google Analytics plugin for Snowplow is copyright 2018-2018 Snowplow Analytics Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.