Skip to content

Commit

Permalink
Fix compatibility issue with React Native environment (#220)
Browse files Browse the repository at this point in the history
* Fix compatibility issue with React Native environment in react-tracking package

* Update README.md
  • Loading branch information
kimskovhusandersen authored May 25, 2023
1 parent bf15e1b commit bb91f41
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ _This is also how you would use this module without `@decorator` syntax, althoug

### Custom `options.dispatch()` for tracking data

By default, data tracking objects are pushed to `window.dataLayer[]` (see [src/dispatchTrackingEvent.js](src/dispatchTrackingEvent.js)). This is a good default if you use Google Tag Manager. You can override this by passing in a dispatch function as a second parameter to the tracking decorator `{ dispatch: fn() }` on some top-level component high up in your app (typically some root-level component that wraps your entire app).
By default, data tracking objects are pushed to `window.dataLayer[]` (see [src/dispatchTrackingEvent.js](src/dispatchTrackingEvent.js)). This is a good default if you use Google Tag Manager.
However, please note that in React Native environments, the window object is undefined as it's specific to web browser environments.
You can override this by passing in a dispatch function as a second parameter to the tracking decorator `{ dispatch: fn() }` on some top-level component high up in your app (typically some root-level component that wraps your entire app).

For example, to push objects to `window.myCustomDataLayer[]` instead, you would decorate your top-level `<App />` component like this:

Expand Down
2 changes: 1 addition & 1 deletion src/dispatchTrackingEvent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function dispatchTrackingEvent(data) {
if (Object.keys(data).length > 0) {
if (typeof window !== 'undefined' && Object.keys(data).length > 0) {
(window.dataLayer = window.dataLayer || []).push(data);
}
}

0 comments on commit bb91f41

Please sign in to comment.