Coder Social home page Coder Social logo

Comments (6)

ttmarek avatar ttmarek commented on May 29, 2024

Sounds good @shayc!

Let me know if you have any questions.

from redux-beacon.

BrendanFDMoore avatar BrendanFDMoore commented on May 29, 2024

Thanks for reporting the issue @shayc 🎉

🙌

from redux-beacon.

mihaiblaga89 avatar mihaiblaga89 commented on May 29, 2024

Any news on this? Thanks

from redux-beacon.

shayc avatar shayc commented on May 29, 2024

@mihaiblaga89 I've been having some packed days lately (moving an apartment), hadn't had much time to work on a PR. Feel free to start your own just let me know, thanks!

from redux-beacon.

ttmarek avatar ttmarek commented on May 29, 2024

For those needing Global Site Tag support here are the steps you can follow as we wait for this to be added to Redux Beacon.

Step 1

Like with the Google Analytics target, the first step is to add the tracking snippet to the head of your single page app's index.html file:

<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());
  gtag('config', 'GA_TRACKING_ID', { 'send_page_view': false });
</script>

⚠️ Be sure to delete the gtag('config', 'GA_TRACKING_ID'); line from the tracking snippet provided by Google Analytics, and replace it with gtag('config', 'GA_TRACKING_ID', { 'send_page_view': false }); where GA_TRACKING_ID is the one provided to you by Google Analytics.

Step 2

Next, copy & paste the following function wherever you are configuring Redux Beacon in your project.

function GoogleSiteTag(gaTrackingId) {
  return function target(events) {
    // Ensure the target works when doing SSR
    if (typeof window === 'undefined') {
      return;
    }
    
    // Ensure the Google Site Tag snippet is available
    if (typeof window.gtag !== 'function') {
      throw new Error('window.gtag is not a function. Did you forget to include the Google Site Tag snippet?');
    }

    // Push the events to Google Site Tag
    events.forEach(event => {
      if (event.type === 'page') {
        gtag('config', gaTrackingId, event);
      } else if (event.type === 'event') {
        gtag('event', event.name, event);
      }
    });
  };
}

Step 3

Then provide this function/target when creating the middleware or a meta reducer:

const gaTrackingId = 'My GA Tracking Id'; // <<< Replace with your tracking Id.
const middleware = createMiddleware(eventsMap, GoogleSiteTag(gaTrackingId));
const metaReducer = createMetaReducer(eventsMap, GoogleSiteTag(gaTrackingId));

Step 4

Finally, in your event definitions if you want to push a page view:

const pageView = (action) => ({
   type: 'page',
   page_title: 'whatever' // probably get this by mapping the route to a map of page titles
   page_location: action.payload.location // or wherever this is in your action
   page_path: action.payload.path // or wherever this is in your action
});

And, if you want to push an event:

const event = (action) => ({
   type: 'event',
   name: 'event_name', // You need this
   'parameter_1': 'value_1',
   'parameter_2': 'value_2',
});

Review

What we've done here is create a custom Redux Beacon target for Google Site Tag. It's a very bare-bones target that I hacked together on my lunch break. I'm leaning on y'all to help me test it out. I do intend to add a similar target to Redux Beacon in the next few days that is a little more robust and feature-rich. Until then, I hope this'll do.

  • I would love a pull request that adds this target to Redux Beacon (with tests and support for multiple tracking Ids)
  • I would also accept a pull request adding documentation around creating custom targets, for scenarios like these where Redux Beacon doesn't expose its own.

Happy analytics tracking 🙂

from redux-beacon.

ttmarek avatar ttmarek commented on May 29, 2024

Finally published! https://rangle.gitbook.io/redux-beacon/targets/googleanalyticsgtag

from redux-beacon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.