<jupyter_start><jupyter_text>Google Analytics - GoogleAnalytics Get stats per country **Tags:** googleanalytics statspercountry Pre-requisite: Create your own Google API JSON credential  Input  Import library<jupyter_code>import pycountry

import plotly.graph_objects as go
import plotly.express as px

from naas_drivers import googleanalytics<jupyter_output><empty_output><jupyter_text>Get your credential from Google Cloud Platform<jupyter_code>json_path = '/Users/charlesdemontigny/Desktop/naas-335023-90c733ba64dd.json'<jupyter_output><empty_output><jupyter_text>Get view id from google analytics<jupyter_code>view_id = "236707574"<jupyter_output><empty_output><jupyter_text>Model  Report Website - Google Analytics performance<jupyter_code>googleanalytics.connect(json_path=json_path)<jupyter_output><empty_output><jupyter_text>Visitor's country of origin<jupyter_code>country = googleanalytics.views.get_data(
            view_id,
            metrics="ga:sessions",
            pivots_dimensions="ga:country",
            dimensions="ga:month",
            start_date=None,
            end_date=None,
            format_type="pivot",
        )
sessions_per_country = googleanalytics.views.get_country(view_id) # default: metrics="ga:sessions"
sessions_per_country
users_per_country = googleanalytics.views.get_country(view_id, metrics="ga:users")<jupyter_output><empty_output><jupyter_text>Output  Display result<jupyter_code>sessions_per_country.head()
users_per_country.head()
sessions_per_country = sessions_per_country.reset_index().rename(columns={"index": "Country"})
mapping = {country.name: country.alpha_3 for country in pycountry.countries}
sessions_per_country['iso_alpha'] = sessions_per_country['Country'].apply(lambda x: mapping.get(x))
sessions_per_country
fig = px.choropleth(sessions_per_country, locations="iso_alpha",
                    color="Sessions", 
                    hover_name="Country",
                    color_continuous_scale="Greens")
fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09717402082300446, 'fraction_numerical': 0.011403073872087258, 'mean_word_length': 5.771812080536913, 'pattern_counts': {'":': 1, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '195903', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 655, 'n_tokens_neox': 634, 'n_words': 96, 'path': 'Google Analytics/GoogleAnalytics_Get_stats_per_country.ipynb', 'repo_name': 'vivard/awesome-notebooks', 'size': '2017'}