How to Define a Cost Fucntion?
well there are two things. 


You write your learning algorithm from scratch. Then you define your cost function yourself. No one usually does that unless they are asked for.
Using Machine Learning libraries in Python (Most common is SKlearn unless you want to use Neural Networks in e.g. Keras). They have their own cost functions designed. You just need to define your problem properly then it does the thing for you. 


In (2) just define your problem properly. The question is fuzzy (what does "not generating traffic good traffic" mean?) but you input your data to your algorithm and it optimize the cost function according to the chosen algorithm. If you need more input, please update the question and give more details so I can help.