If you want to have it all in a single script I think that you´ll have to use a revit Databse transaction, not the transaction manager
you´ll need to import
from Autodesk.Revit.DB import Transaction
and define
tran = Transaction(doc, ‘Name’)
and then use tran.Start() and tran.Commit() instead of transactionmanager.insureTransaction and TaskIsDone