Integrate Google Analytics

Update The Frontity Core team is working in an official integration for Google Analytics. Read the thread carefully.


I share the way I integrated Google Analytics, maybe it will be useful for others, or maybe I can improve it with your helps:

npm install react-ga --save

In your packages/your-theme/src/components/index.js add:

import ReactGA from "react-ga";

const Theme = ({ state, libraries, actions }) => {
  ReactGA.initialize("UA-123456789-1");
  ReactGA.pageview(state.router.link);

That’s it.

2 Likes

Awesome @koli14 . Thanks for sharing this!

What are you building with Frontity? :blush:

My first project was a news site about Hungarian theater: https://szinhaz.online
I’m looking forward to use Frontity again!
My snippet has (at least) one weak point: It logs just the first arrive to the site. If user navigates in it, it does not log. I hope the core team will solve it soon (The `analytics` library).

Actually, the core team is also working on it. There is an npm package, but no doc’s yet. @David can we already use this package? Can you provide the basic steps for it?

Yes, we are working on it! The stable versions are expected to be published in a couple of weeks.

The current analytic packages are still in beta, but you can use them if you want.

I wrote in Using @frontity/analytics a brief explanation of the actual status and how to use them.

Cheers :wave:

2 Likes

Oh, I forgot to say that our recomendation will be to always use the official packages (once they are ready).

We found a problem regarding analytics, and it is that the title may not be ready right after doing actions.router.set(link) (e.g. going to a link that hasn’t been fetched yet) and that would cause pageviews to be sent with the wrong title (remember that pageviews contain title and link).

For that reason we would have to wait for the title to be ready and that wasn’t a problem trivial to solve. If you are curious, you can check our current solution here, in the analytics library.

1 Like

I think I should delete this topic, as the official package works already much better, as my solution. Do you agree?

Hey @koli14, instead of deleting it, it would be much better if you edit the original post with an update explaining the current situation :blush:

Yes, I agree, but I do not see an Edit button on my original post. And if I click on the Edit history, I see that juanma edited it.

1 Like

I just edited it :wink: