0

I am trying to automate export of Amplitude reports and then adding them in G-Spreadsheets using Python but I am getting the below error:

Error: Failed to parse JSON response: Expecting value: line 1 column 1 (char 0)
Failed to fetch data for [RAW] Amplitude Totals - Viewed (.*) Page
Error: Failed to parse JSON response: Expecting value: line 1 column 1 (char 0)
Failed to fetch data for [RAW] Amplitude Totals - Viewed Product Page
Error: Failed to parse JSON response: Expecting value: line 1 column 1 (char 0)
Failed to fetch data for [RAW] Amplitude Totals - Product Added
Error: Failed to parse JSON response: Expecting value: line 1 column 1 (char 0)
Failed to fetch data for [RAW] Amplitude Totals - Checkout Started
Error: Failed to parse JSON response: Expecting value: line 1 column 1 (char 0)
Failed to fetch data for [RAW] Amplitude Totals - Order Completed

I have been trying to make the code work but unfortunately I am stuck at the above error. What am I doing wrong here? Below is the code:

import requests
import base64
import gspread
from google.oauth2 import service_account
import json


def fetch_amplitude_data(api_key, secret_key, start_time, end_time, report_url):
    url = report_url
    params = {
        'start': start_time,
        'end': end_time
    }
    credentials = f"{api_key}:{secret_key}"
    encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
    headers = {
        'Authorization': f'Basic {encoded_credentials}'
    }

    response = response = requests.get(url + '?format=json', params=params, headers=headers)


    if response.status_code == 200:
        try:
            data = response.json()
            return data
        except json.decoder.JSONDecodeError as e:
            print(f"Error: Failed to parse JSON response: {e}")
            return None
    else:
        print(f"Error: Failed to fetch data from Amplitude. Status code: {response.status_code}")
        return None


api_key = ''
secret_key = ''
start_time = '2023-04-01T00:00:00Z'
end_time = '2023-05-16T23:59:59Z'

report_urls = [
    'https://analytics.amplitude.com/magbak/chart/new/coqje4ni',
    'https://analytics.amplitude.com/magbak/chart/new/uxszho05',
    'https://analytics.amplitude.com/magbak/chart/new/edf4hjtn',
    'https://analytics.amplitude.com/magbak/chart/new/vxed7m7e',
    'https://analytics.amplitude.com/magbak/chart/new/mpe5vnf0'
]

tab_names = [
    '[RAW] Amplitude Totals - Viewed (.*) Page',
    '[RAW] Amplitude Totals - Viewed Product Page',
    '[RAW] Amplitude Totals - Product Added',
    '[RAW] Amplitude Totals - Checkout Started',
    '[RAW] Amplitude Totals - Order Completed'
]

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = service_account.Credentials.from_service_account_file('C:/Users/Administrator/Downloads/second-caster-373808-fcf096bc469f.json.', scopes=scope)

client = gspread.authorize(credentials)

spreadsheet = client.open('[MagBak GP Pixel vs MagBak Native Pixel] Audit (April 18, 2023 - May 15, 2023) ')

for i, report_url in enumerate(report_urls):
    amplitude_data = fetch_amplitude_data(api_key, secret_key, start_time, end_time, report_url)
    if amplitude_data is not None:
        # Select the respective tab
        tab = spreadsheet.worksheet(tab_names[i])

        # Clear existing data in the tab
        tab.clear()

        # Update the tab with the fetched data
        tab.update([amplitude_data])
        print(f"Data updated in {tab_names[i]}")
    else:
        print(f"Failed to fetch data for {tab_names[i]}")

Any help would be appreciated. THANKS!

  • Print the response, to see the content type provided and the current body format. It could help to debug – guillaume blaquiere May 17 '23 at 13:57
  • Hello friend, can you provide an example of the JSON object that you are collecting after the response?, could be that the JSON object it is incorrect and the parser it is incorrectly working. – Franco Gil May 17 '23 at 14:44

1 Answers1

0

You have a extra period here: "C:/Users/Administrator/Downloads/second-caster-373808-fcf096bc469f.json."

Verify if is that the cause