Analytics on easy mode with Redux-Saga
Tags: react, redux, reduxsaga, analytics
Published: 2017-11-08 03:44:27

---
title: "Analytics on easy mode with Redux-Saga"
published: true
tags: React, Redux, Redux-Saga, Analytics
canonical_url: https://goshakkk.name/analytics-easy-redux-saga/
---

_This post was originally published on [goshakkk.name](https://goshakkk.name/analytics-easy-redux-saga/)_

Tracking how your users actually use your application can be powerful.

And to do that, you _could_ put the `window.ga(...)` calls all around: inside action creators or your components.

But does that approach strike you as beautiful, in any way?

```js
class SignUpForm extends Component {
  handleSubmit = () => {
    // fire a redux action
    this.props.doSignUp(data).then(() => {
      window.ga('track', 'CompleteRegistration');
    });
  };
}

class AddTodoForm extends Component {
  handleSubmit = () => {
    // fire a redux action
    this.props.createTodo(data).then(() => {
      window.ga('track', 'CreateTodo');
    });
  };
}
```

You can see where it’s going.

Can you do better?

With [Redux-saga](http://redux-saga.js.org), you can. You can place all analytics-related behavior (or most of it, anyway) in a separate file. Which would “listen for certain Redux actions, and automatically track to your analytics service.

But first, what’s Redux-saga about? In its simplest form, _a saga_, the unif of Redux-saga, is a function like this:

```js
function* aSaga() {
  while (true) {
    const action = yield take('ACTION_NAME');
    // do something
  }
}
```

It waits for a certain action to happen, then does something — whetever you want. After that, it would wait for another action to occur, and the cycle would repeat.

You could make a lot more complicated sagas, but these are a great start!

_(Now, if you are wondering what this `function*` and `yield` are — these have to do with ES6 generator functions. I [have a blog post](/javascript-generators-understanding-sample-use-cases/) explaining them in more detail.)_

A more practical example would be:

```js
// analytics-sagas.js

function* registrationAnalytics() {
  while (true) {
    yield take('USER_SIGNED_UP');
    window.ga('track', 'CompleteRegistration');
  }
  }

function* todoAnalytics() {
  while (true) {
    yield take('ADD_TODO');
    window.ga('track', 'CreateTodo');
  }
  }

sagaMiddleware.run(registrationAnalytics);
sagaMiddleware.run(todoAnalytics);

export [
  registrationAnalytics,
  todoAnalytics,
];

// when creating redux store

import analyticsSagas from './analytics-sagas.js';

// ...

analyticsSagas.forEach(saga => sagaMiddleware.run(saga));
```

Look at this code.

The entire functionality of analytics is now contained in one single file.

And instead of making calls to analytics in our components, we can “feed from Redux actions as they happen.

## Considerations

Since sagas can only respond to Redux actions, there still might be places in your app where you would inline analytics calls right inside components. However, even so, you’d be still better off than with inlining everything.

## Other uses for sagas

- [The Lazy Registration pattern](https://goshakkk.name/lazy-auth-redux-saga-flow/).

## References

- [Understanding ES6 generators and their use cases](https://goshakkk.name/javascript-generators-understanding-sample-use-cases/)
 ![](http://feeds.feedburner.com/~r/GoshaArinich/~4/OU3RFcgBplU)

---

If you like what you see here, [subscribe on my blog](http://goshakkk.name/subscribe/) to make sure you don't miss out on my next posts about React and Redux.