def retrieveMktoActivities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #return try: print 'getting mkto activities' #company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id).first() activityTypeIds = [] # if existingIntegration is not None: # activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] # for i in range(len(activityTypeArray)): # if (activityTypeArray[i]['name'] == 'Send Email' or activityTypeArray[i]['name'] == 'Email Delivered' or activityTypeArray[i]['name'] == 'Open Email' or activityTypeArray[i]['name'] == 'Visit Webpage' or activityTypeArray[i]['name'] == 'Fill out Form' or activityTypeArray[i]['name'] == 'Click Link' or activityTypeArray[i]['name'] == 'Email Bounced' or activityTypeArray[i]['name'] == 'Email Unsubscribed' or activityTypeArray[i]['name'] == 'Change Data Value'): # activityTypeIds.append(str(activityTypeArray[i]['id'])) if existingIntegration is not None: activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] for i in range(len(activityTypeArray)): activityTypeIds.append(str(activityTypeArray[i]['id'])) if activityTypeIds is None: return [] if sinceDateTime is None: sinceDateTime = datetime.now() - timedelta(days=30) #change to 365 mkto = Marketo(company_id) #get the 'Claritix Lead List' in order to only get activities for leads in that list listList = mkto.get_lists(id=None , name=['Claritix Leads List'], programName=None, workspaceName=None, batchSize=None) if listList and listList[0]: leadListId = listList[0]['id'] else: raise ValueError('Claritix Leads List not found') batch_size = 10 #10 Activity Types at a time activityList = [] for i in range(0, len(activityTypeIds), batch_size): activityTypeIdsTemp = activityTypeIds[i:i+batch_size] print 'gettng activities for ' + str(activityTypeIdsTemp) activityList.extend(mkto.get_lead_activity(activityTypeIdsTemp, sinceDatetime=sinceDateTime, leadListId=leadListId)) #delete leads from lead list in Marketo deleteList = mkto.get_leads_by_listId(listId=leadListId) deleteLeadIds = [str(e['id']) for e in deleteList] print 'leads to be removed from CX List are ' + str(deleteLeadIds) batch_size = 300 for i in range(0, len(deleteLeadIds), batch_size): mkto.remove_leads_from_list(listId=leadListId, leadsIds = deleteLeadIds[i:i+batch_size]) print 'leads removed from Mkto CX List' print 'going to save mkto activities - count ' + str(len(activityList)) saveMktoActivities(user_id=user_id, company_id=company_id, activityList=activityList, activityTypeArray=activityTypeArray, job_id=job_id, run_type=run_type) try: message = 'Activities retrieved from Marketo' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Activities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return activityList except Exception as e: print 'Error while retrieving activities from Marketo ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def retrieveMktoLeadCreatedActivities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #return try: print 'getting mkto lead created activities' # #company_id = request.user.company_id # existingIntegration = CompanyIntegration.objects(company_id = company_id).first() # activityTypeIds = [] # # if existingIntegration is not None: # # activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] # # for i in range(len(activityTypeArray)): # # if (activityTypeArray[i]['name'] == 'Send Email' or activityTypeArray[i]['name'] == 'Email Delivered' or activityTypeArray[i]['name'] == 'Open Email' or activityTypeArray[i]['name'] == 'Visit Webpage' or activityTypeArray[i]['name'] == 'Fill out Form' or activityTypeArray[i]['name'] == 'Click Link' or activityTypeArray[i]['name'] == 'Email Bounced' or activityTypeArray[i]['name'] == 'Email Unsubscribed' or activityTypeArray[i]['name'] == 'Change Data Value'): # # activityTypeIds.append(str(activityTypeArray[i]['id'])) # # if existingIntegration is not None: # activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] # for i in range(len(activityTypeArray)): # activityTypeIds.append(str(activityTypeArray[i]['id'])) # # if activityTypeIds is None: # return [] activityTypeIds = ['12'] #hard coded Mkto Activity Type for 'New Lead' activityTypeArray = [] # redundant - should be removed later if sinceDateTime is None: sinceDateTime = datetime.now() - timedelta(days=30) #change to 365 mkto = Marketo(company_id) batch_size = 10 #10 Activity Types at a time activityList = [] for i in range(0, len(activityTypeIds), batch_size): activityTypeIdsTemp = activityTypeIds[i:i+batch_size] print 'gettng activities for ' + str(activityTypeIdsTemp) activityList.extend(mkto.get_lead_activity(activityTypeIdsTemp, sinceDatetime=sinceDateTime)) print 'going to save mkto lead created activities - count ' + str(len(activityList)) saveMktoActivities(user_id=user_id, company_id=company_id, activityList=activityList, activityTypeArray=activityTypeArray, job_id=job_id, run_type=run_type) try: message = 'Lead Created Activities retrieved from Marketo' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Activities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return activityList except Exception as e: send_notification(dict( type='error', success=False, message=str(e) ))