Skip to content

Conversation

@cw-david-robinson
Copy link
Collaborator

This PR adds support for publishing historical achievements (we have previously added the same functionality for publishing historical exposures in an earlier PR here). This feature is required because there are many achievements we want to track that occur in third party platforms that cannot be directly integrated with ABSmartly (for example, support request outcomes that are tracked in our CRM software). For these third party achievements we can export the underlying data from the third party platform, and then publish them historically to ABSmartly.

To publish an historic achievement:

  • context must be initialised with historic = True (refer here)
  • achieved_at arg must be passed to Context.track method:
    track(
        goal_name="some_goal",
        properties={
            "key": "value"
        },
        achieved_at=1731625200000
    )
    
  • If no achieved_at timestamp is passed to Context.track then achieved timestamp will default to current time

I have also tested these changes with the following script and verified in the ABSmartly UI that achievements are being published with correct historical timestamp:

import os
from sdk.absmartly_config import ABSmartlyConfig
from sdk.context_config import ContextConfig
from sdk.absmartly import ABSmartly
from sdk.client import Client
from sdk.client_config import ClientConfig
from sdk.default_http_client import DefaultHTTPClient
from sdk.default_http_client_config import DefaultHTTPClientConfig


ABSMARTLY_ENDPOINT = "https://catawiki.absmartly.io/v1"
ABSMARTLY_API_KEY = os.getenv("ABSMARTLY_API_KEY")
ENVIRONMENT = "development"
APPLICATION = "ML"
UNITS = {
    "user_id": "1234"
}
ATTRIBUTES = {
    "locale": "nl"
}
HISTORIC = True

# Exposure data
EXPERIMENT_NAME = "test_python_ab"
EXPOSED_AT = 1731625200000

# Goal data
GOAL_NAME = "goal_test_st"
PROPERTIES = {
    "test": "test"
}
ACHIEVED_AT = 1731625200000

# Create context
client_config = ClientConfig()
client_config.endpoint = ABSMARTLY_ENDPOINT
client_config.api_key = ABSMARTLY_API_KEY
client_config.application = APPLICATION
client_config.environment = ENVIRONMENT
default_client_config = DefaultHTTPClientConfig()
default_client = DefaultHTTPClient(default_client_config)
sdk_config = ABSmartlyConfig()
sdk_config.client = Client(client_config, default_client)
sdk = ABSmartly(sdk_config)
context_config = ContextConfig()
context_config.historic = HISTORIC
context = sdk.create_context(context_config)
context.wait_until_ready()
context.set_units(UNITS)
context.set_attributes(ATTRIBUTES)

# Test publishing historical exposure
treatment = context.get_treatment(
    experiment_name=EXPERIMENT_NAME,
    exposed_at=EXPOSED_AT
)
print(treatment)

# Test publishing historical goal
context.track(
    goal_name=GOAL_NAME,
    properties=PROPERTIES,
    achieved_at=ACHIEVED_AT
)

# Publish and close context
context.publish_async()
context.close()
image

Copy link

@marcio-absmartly marcio-absmartly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks David!

@hermeswaldemarin hermeswaldemarin merged commit 7910b9b into main Nov 22, 2024
1 check passed
@hermeswaldemarin hermeswaldemarin deleted the add-support-for-historical-goals branch November 22, 2024 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants