hello and welome to e.health forum i am not a doctor but volunteer and will advise you to the best of my ability.
i suffer from this and the reason i had blood was due to constipation and slight anal tissue tear.
the other known factor was also due to a hemmarroid or (pile) which bursts when you pass your motion.
if you have had niether of those problems i advice you seek medical attention from your family physician to have it checked out. it may well be something simple but no one should take risks from any blood passed before or after defication.
i know myself i tend to get an itch around my anal area and a pinch before the bowel fully evacuates but the bleeding stops. i hope this has been of help to you as we are here to advise and support you.