@@ -106,7 +106,8 @@ export default LazyAnimation;
106106## Track impressions
107107
108108You can use ` IntersectionObserver ` to track when a user views your element, and
109- fire an event on your tracking service.
109+ fire an event on your tracking service. Consider using the ` useOnInView ` to
110+ trigger changes via a callback.
110111
111112- Set ` triggerOnce ` , to only trigger an event the first time the element enters
112113 the viewport.
@@ -115,22 +116,20 @@ fire an event on your tracking service.
115116- Instead of ` threshold ` , you can use ` rootMargin ` to have a fixed amount be
116117 visible before triggering. Use a negative margin value, like ` -100px 0px ` , to
117118 have it go inwards. You can also use a percentage value, instead of pixels.
118- - You can use the ` onChange ` callback to trigger the tracking.
119119
120120``` jsx
121121import * as React from " react" ;
122- import { useInView } from " react-intersection-observer" ;
122+ import { useOnInView } from " react-intersection-observer" ;
123123
124124const TrackImpression = () => {
125- const { ref } = useInView ({
126- triggerOnce: true ,
127- rootMargin: " -100px 0" ,
128- onChange : (inView ) => {
125+ const ref = useOnInView ((inView ) => {
129126 if (inView) {
130- // Fire a tracking event to your tracking service of choice.
131- dataLayer .push (" Section shown" ); // Here's a GTM dataLayer push
127+ // Fire a tracking event to your tracking service of choice.
128+ dataLayer .push (" Section shown" ); // Here's a GTM dataLayer push
132129 }
133- },
130+ }, {
131+ triggerOnce: true ,
132+ rootMargin: " -100px 0" ,
134133 });
135134
136135 return (
0 commit comments