julika Zen julika Zen - 2 years ago 376
PHP Question

Filter Active Users based on page title- Google Analytics

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: {

Please help me to resolve this issue.

Answer Source

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:

    .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:

      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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download