Search code examples
javascriptphpgoogle-analyticsgoogle-analytics-api

How to obtain selected account ID and property ID with Google Embed API


I'm trying to pull some data out of GA using the Google API for PHP, including the user's selected currency type.

I have working PHP code (in isolation), but in order to pull the user's selected currency data I need to pull the user's Google_Service_Analytics_Profile and I need 3 IDs in total to get the profile: $accountId, $webPropertyId, $profileId.

Here's my PHP code that pulls the data:

    // Create a connection to the GA API service
    $analytics = new Google_Service_Analytics($client);

    // Select the GA profile using the provided IDs
    $ga_profile = $analytics->management_profiles->get($input_data->account_id, $input_data->property_id, $input_data->profile_id);

As can be seen from the source, the Google_Service_Analytics_ManagementProfiles_Resource::get() method requires the three IDs mentioned above.

I want to use the Embed API to show the user a selector to allow them to choose the profile that I show data for and I intend to POST the selected IDs to my PHP code which pulls the data from GA and processes it.

Unfortunately, it seems that the only ID that the Embed API returns is the "View ID", which I understand is the $profileID above, but I'm lost as to how I find the remaining $accountId and $webPropertyId IDs to allow me to pull the "Analytics Profile" through the PHP API.

How do I get the remaining IDs?

Here's my HTML and JS: http://jsbin.com/naxiyomeko/1/edit?html,js,console,output


Solution

  • I was able to get the remaining IDs from the Google Embed API ViewSelector using the following Javascript for the change event:

    viewSelector.on('change', function() {
        ids = {}
        ids['account_id'] = viewSelector.B.fb;
        ids['property_id'] = viewSelector.HC.fb;
        ids['profile_id'] = viewSelector.RF.fb;
        // Do something with `ids` object here...
    });
    

    Updated JS bin: http://jsbin.com/hirapupayi/1/edit?html,js,console,output