Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
docs: add samples for Google Analytics property management methods (#74)
Browse files Browse the repository at this point in the history
* docs: add Admin API samples for account management methods

* update copyright and remove redundant run_sample method

* update noxfile template and set enforce_type_hints=False

* add type annotations

* docs: add Admin API samples for account user link management methods

* fix the copyright string, avoid importing functions from other samples

* docs: add samples for Google Analytics property management methods
  • Loading branch information
ikuleshov committed Jun 3, 2021
1 parent 3f92442 commit bdb85be
Show file tree
Hide file tree
Showing 26 changed files with 1,177 additions and 0 deletions.
63 changes: 63 additions & 0 deletions samples/properties_create.py
@@ -0,0 +1,63 @@
#!/usr/bin/env python

# Copyright 2021 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Analytics Admin API sample application which creates a Google
Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/create
for more information.
"""
# [START analyticsadmin_properties_create]
from google.analytics.admin import AnalyticsAdminServiceClient
from google.analytics.admin_v1alpha.types import Property


def run_sample():
"""Runs the sample."""

# !!! ATTENTION !!!
# Running this sample may change/delete your Google Analytics account
# configuration. Make sure to not use the Google Analytics account ID from
# your production environment below.

# TODO(developer): Replace this variable with your Google Analytics
# account ID (e.g. "123456") before running the sample.
account_id = "YOUR-GA-ACCOUNT-ID"
create_property(account_id)


def create_property(account_id):
"""Creates a Google Analytics 4 property."""
client = AnalyticsAdminServiceClient()
property_ = client.create_property(
property=Property(
parent=f"accounts/{account_id}",
currency_code="USD",
display_name="Test property",
industry_category="OTHER",
time_zone="America/Los_Angeles",
)
)

print("Result:")
print(property_)


# [END analyticsadmin_properties_create]


if __name__ == "__main__":
run_sample()
27 changes: 27 additions & 0 deletions samples/properties_create_test.py
@@ -0,0 +1,27 @@
# Copyright 2021 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

import properties_create


FAKE_ACCOUNT_ID = "1"


def test_properties_create():
# This test ensures that the call is valid and reaches the server, even
# though the operation does not succeed due to permission error.
with pytest.raises(Exception, match="403 The caller does not have permission"):
properties_create.create_property(FAKE_ACCOUNT_ID)
52 changes: 52 additions & 0 deletions samples/properties_delete.py
@@ -0,0 +1,52 @@
#!/usr/bin/env python

# Copyright 2021 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Analytics Admin API sample application which deletes the Google
Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/delete
for more information.
"""
# [START analyticsadmin_properties_delete]
from google.analytics.admin import AnalyticsAdminServiceClient


def run_sample():
"""Runs the sample."""

# !!! ATTENTION !!!
# Running this sample may change/delete your Google Analytics account
# configuration. Make sure to not use the Google Analytics property ID from
# your production environment below.

# TODO(developer): Replace this variable with your Google Analytics 4
# property ID (e.g. "123456") before running the sample.
property_id = "YOUR-GA4-PROPERTY-ID"
delete_property(property_id)


def delete_property(property_id):
"""Deletes the Google Analytics 4 property."""
client = AnalyticsAdminServiceClient()
client.delete_property(name=f"properties/{property_id}")
print("Property deleted")


# [END analyticsadmin_properties_delete]


if __name__ == "__main__":
run_sample()
27 changes: 27 additions & 0 deletions samples/properties_delete_test.py
@@ -0,0 +1,27 @@
# Copyright 2021 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

import properties_delete


FAKE_PROPERTY_ID = "1"


def test_properties_delete():
# This test ensures that the call is valid and reaches the server, even
# though the operation does not succeed due to permission error.
with pytest.raises(Exception, match="403 The caller does not have permission"):
properties_delete.delete_property(FAKE_PROPERTY_ID)
65 changes: 65 additions & 0 deletions samples/properties_firebase_links_create.py
@@ -0,0 +1,65 @@
#!/usr/bin/env python

# Copyright 2021 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Analytics Admin API sample application which creates a Firebase link
for the Google Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/create
for more information.
"""
# [START analyticsadmin_properties_firebase_links_create]
from google.analytics.admin import AnalyticsAdminServiceClient
from google.analytics.admin_v1alpha.types import FirebaseLink


def run_sample():
"""Runs the sample."""

# !!! ATTENTION !!!
# Running this sample may change/delete your Google Analytics account
# configuration. Make sure to not use the Google Analytics account ID from
# your production environment below.

# TODO(developer): Replace this variable with your Google Analytics 4
# property ID (e.g. "123456") before running the sample.
property_id = "YOUR-GA4-PROPERTY-ID"

# TODO(developer): Replace this variable with a Firebase project id.
# This project will be linked to the GA4 property.
firebase_project_id = "YOUR-FIREBASE-PROJECT-ID"

create_firebase_link(property_id, firebase_project_id)


def create_firebase_link(property_id, firebase_project_id):
"""Creates a Firebase link for the Google Analytics 4 property."""
client = AnalyticsAdminServiceClient()
firebase_link = client.create_firebase_link(
parent=f"properties/{property_id}",
firebase_link=FirebaseLink(
project=f"projects/{firebase_project_id}",
maximum_user_access="READ_AND_ANALYZE",
),
)

print("Result:")
print(firebase_link)


# [END analyticsadmin_properties_firebase_links_create]

if __name__ == "__main__":
run_sample()
30 changes: 30 additions & 0 deletions samples/properties_firebase_links_create_test.py
@@ -0,0 +1,30 @@
# Copyright 2021 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

import properties_firebase_links_create


FAKE_PROPERTY_ID = "1"
FAKE_FIREBASE_PROJECT_ID = "1"


def test_properties_firebase_links_create():
# This test ensures that the call is valid and reaches the server, even
# though the operation does not succeed due to permission error.
with pytest.raises(Exception, match="403 The caller does not have permission"):
properties_firebase_links_create.create_firebase_link(
FAKE_PROPERTY_ID, FAKE_FIREBASE_PROJECT_ID
)
59 changes: 59 additions & 0 deletions samples/properties_firebase_links_delete.py
@@ -0,0 +1,59 @@
#!/usr/bin/env python

# Copyright 2021 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Analytics Admin API sample application which deletes the Firebase
link from the Google Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/delete
for more information.
"""
# [START analyticsadmin_properties_firebase_links_delete]
from google.analytics.admin import AnalyticsAdminServiceClient


def run_sample():
"""Runs the sample."""

# !!! ATTENTION !!!
# Running this sample may change/delete your Google Analytics account
# configuration. Make sure to not use the Google Analytics property ID from
# your production environment below.

# TODO(developer): Replace this variable with your Google Analytics 4
# property ID (e.g. "123456") before running the sample.
property_id = "YOUR-GA4-PROPERTY-ID"

# TODO(developer): Replace this variable with your Firebase link ID
# (e.g. "123456") before running the sample.
firebase_link_id = "YOUR-FIREBASE-LINK-ID"

delete_firebase_link(property_id, firebase_link_id)


def delete_firebase_link(property_id, firebase_link_id):
"""Deletes the Firebase link."""
client = AnalyticsAdminServiceClient()
client.delete_firebase_link(
name=f"properties/{property_id}/firebaseLinks/{firebase_link_id}"
)
print("Firebase link deleted")


# [END analyticsadmin_properties_firebase_links_delete]


if __name__ == "__main__":
run_sample()
30 changes: 30 additions & 0 deletions samples/properties_firebase_links_delete_test.py
@@ -0,0 +1,30 @@
# Copyright 2021 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

import properties_firebase_links_delete


FAKE_PROPERTY_ID = "1"
FAKE_FIREBASE_LINK_ID = "1"


def test_properties_firebase_links_delete():
# This test ensures that the call is valid and reaches the server, even
# though the operation does not succeed due to permission error.
with pytest.raises(Exception, match="403 The caller does not have permission"):
properties_firebase_links_delete.delete_firebase_link(
FAKE_PROPERTY_ID, FAKE_FIREBASE_LINK_ID
)

0 comments on commit bdb85be

Please sign in to comment.