I'm facing an unusual problem where the actual property tracking works (initialize via ga.initialize), and we're able to track all page hits and granular events.
However, I'm continuing to see an error on the Webmaster's Search Console which states:
Verification failed for <website> using the Google Analytics method. We could not find any Google Analytics tracking codes on the index page of your site.
* You must be using the asynchronous tracking code.
* Your tracking code should be in the <head> section of your page.
* You must have the "edit" permission for the Analytics web property.
I'm not sure how ga.initialize works under the hood, but when I inspect the <head></head> section of our website I don't see any GA tracking scripts registered. Am I supposed to do this manually _in addition_ to using GA? In that case I would be placing my property ID in two different places, which I don't think is right.
Again, all tracking is working fine; I would just like to get past this error in order to access the webmaster's console.
I believe the ga.initialize('tracking-code') only sets up the library so that subsequent calls that are sent will be sent to the right property.
I had a similar error in my dashboard, it said it could't find any tracking code on the website, after some debugging I found I had to change my use case from the NPM example listed to:
'use strict';
import React from 'react';
import { render } from 'react-dom';
import { Router, browserHistory } from 'react-router';
import ReactGA from 'react-ga';
import routes from './routes';
// Setup Google Analytics page tracking
ReactGA.initialize('my-tracking-code');
browserHistory.listen(function(location) {
ReactGA.set({ page: location.pathname });
ReactGA.pageview(location.pathname);
});
render(
<Router history={browserHistory} routes={routes} />,
document.getElementById('app')
);
The .listen(function(location)) is what actually submits the pageviews and is fired upon each history change/load (fires immediately on loading the site and on each navigation change).
Not sure you have similar use case, but I had a similar error so thought I'd let you know what solved it for me.
@fonsleenaars does that somehow cause the tracking code to be added in the <head> element? I don't see how this code would accomplish that.
Stumbled upon this issue, there are alternative methods to verify the domain, one could for instance add the
<meta name="google-site-verification" content="SITE_VERIFICATION_KEY" />
tag to the head section!
@jonathan-stone apologies for never responding over a year ago, silly notifications from GitHub weren't set correctly. Doubt it's still useful, but what my solution does is initialize the tracking code in the library, then every subsequent call to ReactGa.set or ReactGa.pageview or any of its utility functions, will use that tracking code. So setting those functions to be called whenever react-router handles a navigation change, will fire an analytics event through the library. No <head> code needed!
@raubas as far as I remember this issue wasn't just about the verification (although the warning did indicate that) but the underlying issue is that no tracking code is set, which in turn means no analytics data would be sent along. Your solution will indeed work as an alternative for verifying :)
Yeah I just Googled so I thought it might be useful to write down how I did :)
I use this code to track page views and such!
const initialPage = `${history.location.pathname}${history.location.search}`
const facebookPixel = (typeof fbq === undefined) ? () => {} : () => {}
ReactGA.initialize(process.env.GOOGLE_ANALYTICS_KEY)
ReactGA.pageview(initialPage)
history.listen((location, action) => {
// location is an object like window.location
const currentPage = `${location.pathname}${location.search}`
ReactGA.set({
page: currentPage,
});
ReactGA.pageview(location.pathname + location.search);
facebookPixel('track', 'PageView');
})
I think this is solved now.
Most helpful comment
Stumbled upon this issue, there are alternative methods to verify the domain, one could for instance add the
<meta name="google-site-verification" content="SITE_VERIFICATION_KEY" />tag to the head section!