In this quickstart, you create requests to the Google Marketing Platform Admin API and view the
responses containing the list of Google Analytics accounts linked to the
specified Google Marketing Platform organization.

You can complete this quickstart by using a programming language SDK in your
local environment or the REST API.

## Prerequisites

Completing this quickstart requires you to:

- Set up a Google Cloud project and enable the Google Marketing Platform Admin API
- On your local machine:
  - Install, initialize, and authenticate with the Google Cloud
  - Install the SDK for your language

### Set up a Google Cloud project

Set up your Google Cloud project and enable the Google Marketing Platform Admin API.

Click this button to select or create a new Google Cloud project and
automatically enable the Google Marketing Platform Admin API:
Enable the Google Marketing Platform Admin API

### Set up the Google Cloud

On your local machine, set up and authenticate with the Google Cloud.

1. [Install and initialize](https://developers.google.com/sdk/docs/install) the Google Cloud.

2. If you previously installed the Google Cloud, ensure your
   `gcloud` components are updated by running this command.

   ```bash
   gcloud components update
   ```
3. To authenticate with the Google Cloud, generate a local
   Application Default Credentials (ADC) file by running this command. The web
   flow launched by the command is used to provide your user credentials.

   ```bash
   gcloud auth application-default login --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/marketingplatformadmin.analytics.read"
   ```

   Note how scopes required by the Google Marketing Platform Admin API are specified in the command.

   For more information, see [Set up Application Default Credentials](https://developers.google.com/docs/authentication/provide-credentials-adc).

> [!NOTE]
> **Note:** To avoid providing your project ID to the Google Cloud, you can use the [`gcloud config set`](https://developers.google.com/sdk/gcloud/reference/config/set) command to set a default project and region.

The Google Marketing Platform Admin API requires a quota project, which is not set by default.
To learn how to set your quota project, see [User Credentials Guide](https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds).

### Identify your Google Marketing Platform organization ID

To use the Google Marketing Platform Admin API, you need to identify your Google Marketing Platform
organization ID. Login to the [Google Marketing Platform](https://marketingplatform.google.com/),
go to the **Administration** dialog, select your organization, and note the
**Organization ID** under **Organization details**.

All requests to the Google Marketing Platform Admin API must include the organization ID in the format
of `organizations/ORGANIZATION_ID`.

### Set up the SDK for your programming language

On your local machine, click one of the following tabs to install the SDK for
your programming language.

### Java


[Java client library installation guide](https://github.com/googleapis/google-cloud-java/blob/main/java-marketingplatformadminapi/README.md#quickstart)

### PHP


[PHP client library installation guide](https://github.com/googleapis/php-ads-marketingplatform-admin?tab=readme-ov-file#installation)

### Python


[Python client library installation guide](https://github.com/googleapis/google-cloud-python/blob/main/packages/google-ads-marketingplatform-admin/README.rst#installation)

### Node.js


[Node.js client library installation guide](https://github.com/googleapis/google-cloud-node/tree/main/packages/google-marketingplatform-admin#quickstart)

### .NET


[.NET client library installation guide](https://www.nuget.org/packages/Google.Ads.MarketingPlatform.Admin.V1Alpha/)

### Ruby


[Ruby client library installation guide](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-marketingplatformadmin_v1alpha/OVERVIEW.md#installation)

### REST


Configure your environment variables by entering the following.

1. Replace <var translate="no">`ORGANIZATION_ID`</var> with the ID of your Google Marketing Platform organization.
2. Replace <var translate="no">`PROJECT_ID`</var> with the ID of your Google Cloud project.

<br />

## Make an API call

Now you can use the Google Marketing Platform API to list the Google Analytics
accounts link to the specified Google Marketing Platform organization.
Run the following code to perform your first call to the API:

### Java


Remove calls to `.setPageSize` and `.setPageToken` when running the quickstart.
[java-marketingplatformadminapi/samples/snippets/generated/com/google/ads/marketingplatform/admin/v1alpha/marketingplatformadminservice/listanalyticsaccountlinks/SyncListAnalyticsAccountLinks.java](https://github.com/googleapis/google-cloud-java/blob/main/java-marketingplatformadminapi/samples/snippets/generated/com/google/ads/marketingplatform/admin/v1alpha/marketingplatformadminservice/listanalyticsaccountlinks/SyncListAnalyticsAccountLinks.java) [Open in Cloud Shell](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=java-marketingplatformadminapi/samples/snippets/generated/com/google/ads/marketingplatform/admin/v1alpha/marketingplatformadminservice/listanalyticsaccountlinks/SyncListAnalyticsAccountLinks.java "Open this sample or repository in the Google Cloud Shell Editor") [View on GitHub](https://github.com/googleapis/google-cloud-java/blob/main/java-marketingplatformadminapi/samples/snippets/generated/com/google/ads/marketingplatform/admin/v1alpha/marketingplatformadminservice/listanalyticsaccountlinks/SyncListAnalyticsAccountLinks.java)

```java
import com.google.ads.marketingplatform.admin.v1alpha.AnalyticsAccountLink;
import com.google.ads.marketingplatform.admin.v1alpha.ListAnalyticsAccountLinksRequest;
import com.google.ads.marketingplatform.admin.v1alpha.MarketingplatformAdminServiceClient;
import com.google.ads.marketingplatform.admin.v1alpha.OrganizationName;

public class SyncListAnalyticsAccountLinks {

  public static void main(String[] args) throws Exception {
    syncListAnalyticsAccountLinks();
  }

  public static void syncListAnalyticsAccountLinks() throws Exception {
    // This snippet has been automatically generated and should be regarded as a code template only.
    // It will require modifications to work:
    // - It may require correct/in-range values for request initialization.
    // - It may require specifying regional endpoints when creating the service client as shown in
    // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
    try (MarketingplatformAdminServiceClient marketingplatformAdminServiceClient =
        MarketingplatformAdminServiceClient.create()) {
      ListAnalyticsAccountLinksRequest request =
          ListAnalyticsAccountLinksRequest.newBuilder()
              .setParent(OrganizationName.of("[ORGANIZATION]").toString())
              .setPageSize(883849137)
              .setPageToken("pageToken873572522")
              .build();
      for (AnalyticsAccountLink element :
          marketingplatformAdminServiceClient.listAnalyticsAccountLinks(request).iterateAll()) {
        // doThingsWith(element);
      }
    }
  }
}
```

<br />

### PHP


[samples/V1alpha/MarketingplatformAdminServiceClient/list_analytics_account_links.php](https://github.com/googleapis/php-ads-marketingplatform-admin/blob/main/samples/V1alpha/MarketingplatformAdminServiceClient/list_analytics_account_links.php) [Open in Cloud Shell](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/php-ads-marketingplatform-admin&page=editor&open_in_editor=samples/V1alpha/MarketingplatformAdminServiceClient/list_analytics_account_links.php "Open this sample or repository in the Google Cloud Shell Editor") [View on GitHub](https://github.com/googleapis/php-ads-marketingplatform-admin/blob/main/samples/V1alpha/MarketingplatformAdminServiceClient/list_analytics_account_links.php)

```php
use Google\Ads\MarketingPlatform\Admin\V1alpha\AnalyticsAccountLink;
use Google\Ads\MarketingPlatform\Admin\V1alpha\Client\MarketingplatformAdminServiceClient;
use Google\Ads\MarketingPlatform\Admin\V1alpha\ListAnalyticsAccountLinksRequest;
use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;

/**
 * Lists the Google Analytics accounts link to the specified Google Marketing
 * Platform organization.
 *
 * @param string $formattedParent The parent organization, which owns this collection of Analytics
 *                                account links. Format: organizations/{org_id}
 *                                Please see {@see MarketingplatformAdminServiceClient::organizationName()} for help formatting this field.
 */
function list_analytics_account_links_sample(string $formattedParent): void
{
    // Create a client.
    $marketingplatformAdminServiceClient = new MarketingplatformAdminServiceClient();

    // Prepare the request message.
    $request = (new ListAnalyticsAccountLinksRequest())
        ->setParent($formattedParent);

    // Call the API and handle any network failures.
    try {
        /** @var PagedListResponse $response */
        $response = $marketingplatformAdminServiceClient->listAnalyticsAccountLinks($request);

        /** @var AnalyticsAccountLink $element */
        foreach ($response as $element) {
            printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString());
        }
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * Helper to execute the sample.
 *
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $formattedParent = MarketingplatformAdminServiceClient::organizationName('[ORGANIZATION]');

    list_analytics_account_links_sample($formattedParent);
}
```

<br />

### Python


[packages/google-ads-marketingplatform-admin/samples/generated_samples/marketingplatformadmin_v1alpha_generated_marketingplatform_admin_service_list_analytics_account_links_sync.py](https://github.com/googleapis/google-cloud-python/blob/main/packages/google-ads-marketingplatform-admin/samples/generated_samples/marketingplatformadmin_v1alpha_generated_marketingplatform_admin_service_list_analytics_account_links_sync.py) [Open in Cloud Shell](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-python&page=editor&open_in_editor=packages/google-ads-marketingplatform-admin/samples/generated_samples/marketingplatformadmin_v1alpha_generated_marketingplatform_admin_service_list_analytics_account_links_sync.py "Open this sample or repository in the Google Cloud Shell Editor") [View on GitHub](https://github.com/googleapis/google-cloud-python/blob/main/packages/google-ads-marketingplatform-admin/samples/generated_samples/marketingplatformadmin_v1alpha_generated_marketingplatform_admin_service_list_analytics_account_links_sync.py)

```python
# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
#   client as shown in:
#   https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.ads import marketingplatform_admin_v1alpha


def sample_list_analytics_account_links():
    # Create a client
    client = marketingplatform_admin_v1alpha.MarketingplatformAdminServiceClient()

    # Initialize request argument(s)
    request = marketingplatform_admin_v1alpha.ListAnalyticsAccountLinksRequest(
        parent="parent_value",
    )

    # Make the request
    page_result = client.list_analytics_account_links(request=request)

    # Handle the response
    for response in page_result:
        print(response)

```

<br />

### Node.js


Usage: node packages/google-marketingplatform-admin/samples/quickstart.js organizations/ORGANIZATION_ID.
[packages/google-marketingplatform-admin/samples/quickstart.js](https://github.com/googleapis/google-cloud-node/blob/main/packages/google-marketingplatform-admin/samples/quickstart.js) [Open in Cloud Shell](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-node&page=editor&open_in_editor=packages/google-marketingplatform-admin/samples/quickstart.js "Open this sample or repository in the Google Cloud Shell Editor") [View on GitHub](https://github.com/googleapis/google-cloud-node/blob/main/packages/google-marketingplatform-admin/samples/quickstart.js)

```javascript
  /**
   * This snippet has been automatically generated and should be regarded as a code template only.
   * It will require modifications to work.
   * It may require correct/in-range values for request initialization.
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  Required. The parent organization, which owns this collection of Analytics
   *  account links. Format: organizations/{org_id}
   */
  // const parent = 'abc123'
  /**
   *  Optional. The maximum number of Analytics account links to return in one
   *  call. The service may return fewer than this value.
   *  If unspecified, at most 50 Analytics account links will be returned. The
   *  maximum value is 1000; values above 1000 will be coerced to 1000.
   */
  // const pageSize = 1234
  /**
   *  Optional. A page token, received from a previous ListAnalyticsAccountLinks
   *  call. Provide this to retrieve the subsequent page.
   *  When paginating, all other parameters provided to
   *  `ListAnalyticsAccountLinks` must match the call that provided the page
   *  token.
   */
  // const pageToken = 'abc123'

  // Imports the Admin library
  const {MarketingplatformAdminServiceClient} =
    require('@google-ads/marketing-platform-admin').v1alpha;

  // Instantiates a client
  const adminClient = new MarketingplatformAdminServiceClient({fallback: true});

  async function callListAnalyticsAccountLinks() {
    // Construct request
    const request = {
      parent,
    };

    // Run request
    const iterable = adminClient.listAnalyticsAccountLinksAsync(request);
    for await (const response of iterable) {
      console.log(response);
    }
  }

  callListAnalyticsAccountLinks();
```

<br />

### .NET


[apis/Google.Ads.MarketingPlatform.Admin.V1Alpha/Google.Ads.MarketingPlatform.Admin.V1Alpha.GeneratedSnippets/MarketingplatformAdminServiceClient.ListAnalyticsAccountLinksSnippet.g.cs](https://github.com/googleapis/google-cloud-dotnet/blob/main/apis/Google.Ads.MarketingPlatform.Admin.V1Alpha/Google.Ads.MarketingPlatform.Admin.V1Alpha.GeneratedSnippets/MarketingplatformAdminServiceClient.ListAnalyticsAccountLinksSnippet.g.cs) [Open in Cloud Shell](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-dotnet&page=editor&open_in_editor=apis/Google.Ads.MarketingPlatform.Admin.V1Alpha/Google.Ads.MarketingPlatform.Admin.V1Alpha.GeneratedSnippets/MarketingplatformAdminServiceClient.ListAnalyticsAccountLinksSnippet.g.cs "Open this sample or repository in the Google Cloud Shell Editor") [View on GitHub](https://github.com/googleapis/google-cloud-dotnet/blob/main/apis/Google.Ads.MarketingPlatform.Admin.V1Alpha/Google.Ads.MarketingPlatform.Admin.V1Alpha.GeneratedSnippets/MarketingplatformAdminServiceClient.ListAnalyticsAccountLinksSnippet.g.cs)

```
    using Google.Ads.MarketingPlatform.Admin.V1Alpha;
    using Google.Api.Gax;
    using System;

    public sealed partial class GeneratedMarketingplatformAdminServiceClientSnippets
    {
        /// <summary>Snippet for ListAnalyticsAccountLinks</summary>
        /// <remarks>
        /// This snippet has been automatically generated and should be regarded as a code template only.
        /// It will require modifications to work:
        /// - It may require correct/in-range values for request initialization.
        /// - It may require specifying regional endpoints when creating the service client as shown in
        ///   https://cloud.google.com/dotnet/docs/reference/help/client-configuration#endpoint.
        /// </remarks>
        public void ListAnalyticsAccountLinks()
        {
            // Create client
            MarketingplatformAdminServiceClient marketingplatformAdminServiceClient = MarketingplatformAdminServiceClient.Create();
            // Initialize request argument(s)
            string parent = "organizations/[ORGANIZATION]";
            // Make the request
            PagedEnumerable<ListAnalyticsAccountLinksResponse, AnalyticsAccountLink> response = marketingplatformAdminServiceClient.ListAnalyticsAccountLinks(parent);

            // Iterate over all response items, lazily performing RPCs as required
            foreach (AnalyticsAccountLink item in response)
            {
                // Do something with each item
                Console.WriteLine(item);
            }

            // Or iterate over pages (of server-defined size), performing one RPC per page
            foreach (ListAnalyticsAccountLinksResponse page in response.AsRawResponses())
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (AnalyticsAccountLink item in page)
                {
                    // Do something with each item
                    Console.WriteLine(item);
                }
            }

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int pageSize = 10;
            Page<AnalyticsAccountLink> singlePage = response.ReadPage(pageSize);
            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (AnalyticsAccountLink item in singlePage)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
        }
    }
```

<br />

### REST


To send this request, run the curl command from the command line or
include the REST call in your application.

```bash
curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \\
  -H "x-goog-user-project: ${PROJECT_ID}"
  -H "Content-Type: application/json" \\
  https://marketingplatformadmin.googleapis.com/v1alpha/organizations/${ORGANIZATION_ID}/analyticsAccountLinks
```

<br />

The sample code prints a response with a list of Google Analytics accounts
linked to the specified Google Marketing Platform organization:

```json
{
  "analyticsAccountLinks": [
    {
      "name": "organizations/0a123456789-wxyz/analyticsAccountLinks/1234567890",
      "analyticsAccount": "analyticsadmin.googleapis.com/accounts/1234567890",
      "displayName": "Demo Account",
      "linkVerificationState": "LINK_VERIFICATION_STATE_VERIFIED"
    }
  ]
}
```

Congratulations! You've sent your first request to the Google Marketing Platform API.