问题
I'm using the following code
_gaq.push(['_setCustomVar',1,'logged-in','administrator',1],['_trackPageview']);
to track logged-in user levels on my site (running WordPress).
Now my problem is that in the last month I had around 100 new registrations, but Google Analytics only shows me 65 registered users active on my site.
Is this an error in interpreting the results or am I doing something wrong?
回答1:
PREAMBLE
With Google Analytics(GA), it's impossible to get ALL your hits registred. This is not a GA specific problem. Any analytics tool can be affected.
Imagine for instance, that the user gets its connection broken while registring on your WP site. Chances are that the javascript code won't be executed. The new user will be registred hopefully, but GA won't be notified.
WORKAROUND
This workaround would be to tied the notification of GA with your registration process right on the server side:
Step 1: Register a php function with the user_register hook.
add_action('user_register', 'myplugin_registration_save'); function myplugin_registration_save($user_id) { // GA will be notified here ... }
Step 2: Inside this registred function notify GA of the new user registration.
Here comes php-ga. It's a GA client written in PHP. You can implement nearly every parameter and tracking feature of the original GA Javascript client. Call it to track your GA custom vars.
Here is a sample code from the php-ga site:
use UnitedPrototype\GoogleAnalytics;
// Initilize GA Tracker
$tracker = new GoogleAnalytics\Tracker('UA-12345678-9', 'example.com');
// Assemble Visitor information
// (could also get unserialized from database)
$visitor = new GoogleAnalytics\Visitor();
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']);
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$visitor->setScreenResolution('1024x768');
// Assemble Session information
// (could also get unserialized from PHP session)
$session = new GoogleAnalytics\Session();
// Assemble Page information
$page = new GoogleAnalytics\Page('/page.html');
$page->setTitle('My Page');
// Track page view
$tracker->trackPageview($page, $session, $visitor);
THERE IS MORE
Even if you implement the workaround, it may not be able to notify GA sucessfully. If GA server experiment some heavy load for instance, your notification may be lost.
I would advice you to use both system notification : GA Javascript client AND GA PHP client.
Assign them two distinct events 'JS-new-user-registred' and 'PHP-new-user-registred'. With two registred events you improve the quality of your analytic data. You improve the new registrations notification rate too.
Always keep in mind that this approach may not be 100% accurate. For example, on the client side the ga.js
file may be blocked (firewall etc). At the same time, your PHP client may not be able to notify succesfully GA. It results that the new registration is not tracked.
来源:https://stackoverflow.com/questions/16560994/why-google-analytics-doesnt-track-all-my-events-with-setcustomvar