Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
matomo integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-brazhenko committed Mar 17, 2024
1 parent bf8d953 commit 4389faa
Showing 1 changed file with 254 additions and 0 deletions.
254 changes: 254 additions & 0 deletions examples/matomo integration.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You need this if you are running the notebook from repo"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(\"../\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Please note, that it is necessary to adjust this notebook according to your Matomo configuration and data structure. Please check Readme file to check the data structure expectation"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import pandas as pd\n",
"\n",
"from variatio import VariatioAnalyzer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Configuration for Matomo API access\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"MATOMO_URL = 'https://your-matomo-domain.com' # Replace with your Matomo domain\n",
"TOKEN_AUTH = 'your_matomo_api_token' # Replace with your API token\n",
"SITE_ID = 'your_site_id' # Replace with your site ID in Matomo\n",
"DATE_RANGE = '2023-01-01,2023-01-31' # Define the date range for data extraction\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def fetch_from_matomo(method, params):\n",
" \"\"\"\n",
" Generic function to fetch data from the Matomo API.\n",
" - method: Matomo API method to be called\n",
" - params: Additional parameters for the API call\n",
" Returns: JSON response from the API\n",
" \"\"\"\n",
" api_params = {\n",
" 'module': 'API',\n",
" 'method': method,\n",
" 'idSite': SITE_ID,\n",
" 'period': 'range',\n",
" 'date': DATE_RANGE,\n",
" 'format': 'JSON',\n",
" 'token_auth': TOKEN_AUTH,\n",
" **params,\n",
" }\n",
" response = requests.get(MATOMO_URL, params=api_params)\n",
" return response.json()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fetching event data\n",
"Here we're fetching event categories, adjusting parameters as needed for your events\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event_data_response = fetch_from_matomo('Events.getCategory', {'secondaryDimension': 'eventName'})\n",
"event_data_df = pd.json_normalize(event_data_response) # Normalize JSON response into a flat table\n",
"event_data_df = event_data_df[['label', 'nb_visits', 'nb_events']] # Select relevant columns\n",
"event_data_df.columns = ['event_name', 'visits', 'events_count'] # Rename columns for clarity\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fetching A/B test allocation events example\n",
"Assuming A/B test allocations are events with a specific naming pattern\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ab_test_response = fetch_from_matomo('Events.getCategory', {'secondaryDimension': 'eventAction', 'label': 'ABTest Allocation'})\n",
"ab_test_df = pd.json_normalize(ab_test_response)\n",
"ab_test_df = ab_test_df[['label', 'subtable']] # 'subtable' might contain more detailed data requiring further processing\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fetching user properties example (custom dimensions)\n",
"Replace 'ID_OF_YOUR_DIMENSION' with the actual ID of the custom dimension representing user properties\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"user_properties_response = fetch_from_matomo('CustomDimensions.getCustomDimension', {'idDimension': 'ID_OF_YOUR_DIMENSION'})\n",
"user_properties_df = pd.json_normalize(user_properties_response) # Flatten the JSON response into a DataFrame\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"analyzer = VariatioAnalyzer(event_data: event_data_df, \n",
" ab_test_allocations: ab_test_df, \n",
" \"A\", \n",
" user_properties: user_properties_df)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Metrics were calculated\n"
]
}
],
"source": [
"# Calculate the count of 'purchase' events per user\n",
"analyzer.calculate_event_count_per_user('purchase')\n",
"\n",
"# Calculate the sum of 'purchase_value' for 'purchase' events per user\n",
"analyzer.calculate_event_attribute_sum_per_user('purchase', 'purchase_value')\n",
"\n",
"# Calculate the conversion rate to 'login' events\n",
"analyzer.calculate_conversion('login')\n",
"\n",
"print(\"Metrics were calculated\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"analyzer.save_report(\"abtest_report.html\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 4389faa

Please sign in to comment.