Christian Brevik

Google Tag Manager and A/B-testing with React Native

April 24, 2016

It’s kind of funny how sometimes an open source project never really stops evolving.

It definitely should though, don’t get me wrong. There is value in saying a project is “done”. But, my React Native bridge for Google Analytics isn’t quite there yet.

In the latest version I have added a simple Google Tag Manager (GTM) implementation which allows you to retrieve values from a container. The added benefit here is that you can connect your GTM containers to your Google Analytics (GA) views, and perform “Content Experiments”. In other words, A/B-testing.

The guide from Google on how to do the above can be found here. And while it seems comprehensive it is really quite simple.

What you do, after linking your GTM and GA, is setup a “Google Analytics Content Experiment” at GTM, with a specific goal (defined at your GA view) as the experiment objective.

At the same time you also define experiment variations (in JSON) which will serve as the A/B testing parameters. It might look something like: Content Experiment variations

In this scenario, your users will be given either the A_pack or B_pack by GTM. The values from the JSON are then the same values which can be retrieved from the GTM container on the device.

Here is where my library comes in. With the newest version, retrieving parameters from the container can be done as simple as:

import GoogleAnalytics, {
} from 'react-native-google-analytics-bridge'
  .then(() => GoogleTagManager.stringForKey('pack'))
  .then(str => console.log('Pack: ', str))

You can then use these parameters to change the app experience/flow depending on what you are attempting to test.

The experiment objective in the scenario above is linked to a goal which triggers on my Ecommerce/Purchase event: Ecommerce Purchase goal setup

So all I need to do in order to in order to say that a goal has been completed for a specific user, is to track this event through my library:

GoogleAnalytics.trackEvent('Ecommerce', 'Purchase')

Eventually the experiment will announce a winner between my A_pack and B_pack (in my GA view under Behavior -> Experiements), and the A/B-testing for this scenario is concluded.

Hope this blog post has been helpful, visit the Github repo for an installation guide and API.

Christian Brevik

Hi there! I'm a developer living in Trondheim. Find me on Github or Twitter.