amplitude/Amplitude-Java

Trying to create a simple Java example

sgpennebaker opened this issue · 4 comments

Trying to create a simple Java example

package com.carbon.amplitude;

import com.amplitude.Amplitude;
import com.amplitude.Event;

public class AmplitudeSend {

    public static void main( String[] args ) {

        System.out.println( "Hello World!" );

        Amplitude amplitude = Amplitude.getInstance( "WHAT_SHOULD_THIS_BE");
        amplitude.init("I_DO_HAVE_THE_API_KEY" );

        amplitude.logEvent( new Event("ButtonClicked", "test_user_id" ) );
    }
}

Can't figure out what the instance should be. I do have an API key which is in the code. The test project has a project id and a secret key if that helps any.

        Amplitude amplitude = Amplitude.getInstance();

No argument is in the quickstart but that's not legal.

So: what should the instance name be? can that be clarified in the quickstart? can the no argument getInstance() be taken out of the quickstart?

Thanks!

@sgpennebaker
Hi there, the instance name can be whatever you want that to be.
I just tried to reproduce on my end.
I'm able to log the event by using the following code.

Amplitude amplitude = Amplitude.getInstance();
amplitude.init("TEST_API-KEY");
amplitude.logEvent(new Event("ButtonClicked", "test_user_id"));

May I know the error message when you pass no argument?

Hello @sgpennebaker ,

My bad. I just realized this method getInstance() is not available in the first version of the Java SDK. getInstance() is the equivalent of getInstance(""). No string or an empty string represents the default instance. Please update the Java SDK to version 1.2.0 and the quick start should work. Thank you for helping us improve our API. If there's any more questions please reopen this ticket or create a new one.

Dante

awesome - and i apologize, other projects interfered so i am just getting back to this today. so our timing is perfect ;)

nope, sorry, i'm back and still failing. replaced the jar w java-sdk-1.2.0

the code:

import com.amplitude.Amplitude;
import com.amplitude.Event;

public class AmplitudeSend {

    public static void main( String[] args ) {

        System.out.println( "Hello World!" );

        Amplitude amplitude = Amplitude.getInstance();
        amplitude.init("LET ME KNOW IF YOU NEED THIS" );

        amplitude.logEvent( new Event("ButtonClicked", "test_user_id" ) );
    }
}

the console looks fine.

i go and look at my test project in the Amplitude dash and the count does not increment.

i didn't mention this before but i am able to do curl:

#!/bin/bash
curl --header "Content-Type: application/json" \
  --header "Accept: */*" \
  --request POST \
  --data '{ "api_key": "LET ME KNOW IF YOU NEED THIS", "events": [ { "user_id": "datamonster@gmail.com", "device_id": "C8F9E604-F01A-4BD9-95C6-8E5357DF265D", "event_type": "watch_tutorial", "time": 1622783058 } ] }' \
  https://api2.amplitude.com/2/httpapi

this succeeds and the event count does implement.

if it's helpful, the client is Carbon Health and the project is SGP Test.

thanks in advance!