Search code examples
phpgoogle-analyticsgoogle-analytics-api

Get most commonly visited second page for a given page


I'm curious to know if it is possible to be able to get the most visited second page for a specific page URL.

For example, from Page A, can I use the Google Analytics (GA) API to find get the most frequently visited page which visitors navigate towards next?

In other words, I would like to use GA API to tell me that 60% of users navigate towards Page B immediately after Page A. And if a user is on Page C, they have the greatest chance of visiting Page D next.

I am using the PHP library:

$analytics->data_ga->get(
   'ga:' . $profileId,
   '30daysAgo',
   'today',
   'ga:secondPagePath',
    array(
      'filters' => 'ga:pageTitle%3D%3Dabout-us'
    )
);

In the above example I am trying to find the most popular second page from the "about-us" URL. Thanks in advance!


Solution

  • Best way to find relevant metrics and dimensions is to use this:
    https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/

    enter image description here

    You need to bear in mind the dimension scope: so I think you should read the below page:
    https://support.google.com/analytics/answer/2709828?hl=en

    Now to your question: ga:pageTitle is a hit-level dimension (tracked for each page), whereas ga:secondPagePath is session-level dimension collected only once per session, see description given in explorer, thus the query you're trying to run won't work.

    ga:secondPagePath: The second page in users' sessions.

    Also, you cannot query a previous/next page combination using ga:pageTitle since the filtering is done on the previous page (see below) for which there is no dimension available.

    Instead you should query on ga:pagePath (= next page) with filter on ga:previousPagePath (= previous page).

    To save time, use the query explorer and port your queries to PHP once you have them working:
    https://ga-dev-tools.appspot.com/query-explorer/