1

I need to show active visitors on my website based on Page title. Example : If the user is Contact Us page, then onlycount of active visitors on 'Contact us' page is to be shown instead of entire site. I am following Embed API Third Party Visualizations

I tried the following code but it is not working

var activeUsers = new gapi.analytics.ext.ActiveUsers({
  container: 'active-users-container',
  pollingInterval: 5,
  query: {
    filters:'ga:pageTitle==Contact'
  }
});

Please help me to resolve this issue.

Philip Walton
  • 26,214
  • 15
  • 55
  • 81
julika Zen
  • 395
  • 4
  • 21

2 Answers2

1

As I mentioned in the Github issue opened about this same question, what you're asking to do is not possible with the current ActiveUsers Embed API component (it's just for demo purposes, it's not intended to be a full-featured Real Time query component).

However, this component could easily be modified to support your use case. You'd just have to update the lines that make the API request to add your query parameters.

Here is the relevant line of code that makes the request:

gapi.client.analytics.data.realtime
    .get({ids: options.ids, metrics: 'rt:activeUsers'})
    .then(function(response) {
       // ... do something with the response
    });

To add your filter, you'd just have to change that line to something like this:

gapi.client.analytics.data.realtime
    .get({
      ids: options.ids,
      metrics: 'rt:activeUsers',
      filters: 'ga:pageTitle==Contact'
    })
    .then(function(response) {
       // ... do something with the response
    });

Of course, you'd probably rather refactor the component to accept an options object (as you show in your question), so you don't have to hard code your query, but I'll leave it up to you to make that code change.

To point you in the right direction, you should read the Embed API guide: Creating Custom Components. You'll probably also need to reference the Shared Component Reference to see the methods all components can call.

Philip Walton
  • 26,214
  • 15
  • 55
  • 81
0

I tried this Show active visitors after filtering

it worked.

julika Zen
  • 395
  • 4
  • 21