peaksandpies / universal-analytics

A node module for Google's Universal Analytics and Measurement Protocol
960 stars 146 forks source link

Real-time from server-side? #104

Open tashburn opened 6 years ago

tashburn commented 6 years ago

I'm starting to use universal-analytics exclusively on the server-side. My browser clients send custom events to my servers, and my servers then post events to google analytics using universal-analytics.

I'd love to somehow accomplish GA's "real-time" abilities with my setup, where I know real-time info about clients. Can this be done with universal-analytics from the server-side?

If universal-analytics is already capable and I missed it in the documentation, please let me know how to do it.

ClickoBito commented 6 years ago

I'm currently facing the same issue trying to post data from the server to avoid ad-blockers. pageview() works fine and the data shows up in the real-time list of active pages, but data posted with event() does not show up anywhere as far as I can tell, even if I use "Daisy-chaining" to post the event together with a pageview.

Did you have any luck solving this @tashburn ?

EDIT: The event data is displayed in the report the day after, they just do not show up in the real time view. But somehow only the events label shows up, the category and action do not...

EDIT2: Sending events using postman works and they are displayed in the real time view. Example post request: www.google-analytics.com/collect?v=1&t=event&tid=UA-XXXXXXXXX-1&cid=22981763-620f-4d2b-98c7-3c9f536d94be&ec=video&ea=play&el=holiday&ev=300 where UA-XXXXXXXXX-1 should be your Google analytics account id.

Using postman putting the data in the body as plain text also works and the event is displayed in the real-time view. Example: https://www.google-analytics.com/collect With Body: v=1&t=event&tid=UA-XXXXXXXXX-1&cid=22981763-620f-4d2b-98c7-3c9f536d94be&ec=video_postman&ea=play&el=holiday&ev=300

jtillmann commented 6 years ago

Could either of you post the exact call of the module for tracking the events that do not show up in Google Analytic's real-time reports?

MrOrz commented 6 years ago

I also encountered this issue.

Steps to reprodue

// ga-test.js
const ua = require('universal-analytics');

const visitor = ua('<My UA ID>');
visitor
  .event({
    ec: 'Fake',
    ea: 'Fake action',
  })
  .send();
  1. open up the real time report on google analytics. This should reactivate the real time view.
  2. execute the code with node ga-test.js

Expected

After some short delay (< 20s), a event shows up in real time report (under Reports > Real-time > Events)

Actual

No events shown in the real time report on google anaytics.

Other observations

When request's debug flags are turned on (by DEBUG=universal-analytics NODE_DEBUG=request), I can see the request body being:

ec=Fake&ea=Fake%20action&v=1&tid=<My UA ID>&cid=<random UUID>&t=event

It looks totally legit.

MrOrz commented 6 years ago

Hi,

I found that if I send the action along with screenview, then it will show up. Maybe pageview will have the same effect.

Reproduce:

const ua = require('universal-analytics');

const visitor = ua('<UA ID>');

visitor.screenview({
  screenName: 'FakeScreen',
  applicationName: 'Fake app',
});

visitor
  .event({
    ec: 'Fake',
    ea: 'Fake action',
  })
  .send();
  1. Open up Google analytics realtime report
  2. Replace <UA ID> with yours
  3. Execute

Then you should see the pageview & event coming to the report.

Vadorequest commented 5 years ago

I haven't had any issue using this from the server side the following way:

const visitor = ua(this.googleAnalyticsTrackingId, this.sessionId, {
        uid: this.deviceId,
      });

      visitor.event(
        this.getGoogleAnalyticsCategory(),
        this.getGoogleAnalyticsAction(),
        this.getGoogleAnalyticsLabel(),
        this.getGoogleAnalyticsValue(),
        this.getGoogleAnalyticsPath(),
        (err) => {
          if (err) {
            this.logger.error(JSON.stringify(err, null, 2), 'sendGoogleAnalyticsStatistics');
            Raven.captureMessage(err, { level: 'error' });
          }
        },
      );

Maybe it doesn't behave correctly using the .event(object).send(), I use the callback way

alex-controlx commented 4 years ago

Same here, no real-time data. Just sending a got.post() request makes it appearing in real-time GA report page.

await got.post('https://www.google-analytics.com/collect?v=1&t=pageview&tid=UA-XXXXXXXX-XX&cid=22981763-620f-4d2b-98c7-3c9f536d94be&dp=video')