Created
July 9, 2019 21:53
-
-
Save nicogaspa/999f15d0c701de0e2a3b75b4b2450081 to your computer and use it in GitHub Desktop.
simple class with function to upload offline events in an offline event set using facebook python sdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from facebook_business.adobjects.offlineconversiondataset import OfflineConversionDataSet | |
from facebook_business.api import FacebookAdsApi | |
class FacebookService: | |
def __init__(self): | |
self.api = FacebookAdsApi.init(app_id='your_app_id', app_secret='your_app_secret', | |
access_token='your_access_token') | |
self.offline_dataset = OfflineConversionDataSet('offline_set_id') | |
def upload_offline_conversion(self, csv_filename): | |
df = pd.read_csv(csv_filename, sep=";", dtype=object) | |
# df columns are 'order_id', 'value', 'event_time', 'event_name', 'email', 'phone', 'fn', 'ln', 'currency' | |
df['value'] = pd.to_numeric(df['value']) | |
# event times have to be sent in UNIX timestamp format | |
df['event_time'] = (pd.to_datetime(df['event_time']).astype(int) / 10 ** 9).astype(int).astype(str) | |
df['match_keys'] = df.apply(lambda row: json.dumps({k: [row[k]] if k in ['email', 'phone'] else row[k] for k in ['email', 'phone', 'fn', 'ln'] if pd.notnull(row[k])}), axis=1) | |
del df['email'] # deleting match_keys single columns since they are now useless | |
del df['phone'] | |
del df['fn'] | |
del df['ln'] | |
data = df.to_dict(orient="records") | |
batch_limit = 2000 # Maximum number of events permitted in a single call | |
for i in range(0, len(data), step=batch_limit): | |
params = { | |
'upload_tag': 'purchases_upload', # This must be a string, unique for all your uploads, that will let you identify them | |
'data': data[i:i+batch_limit], | |
} | |
self.offline_dataset.create_event(params=params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately I haven't tried but if there is, try looking at this file of the official sdk, since FB documentation is often vague.
https://github.com/facebook/facebook-python-business-sdk/blob/a53c8ba0e8f7d0b41b385c60089f6ba00fa5c814/facebook_business/adobjects/offlineconversiondataset.py