最近对接了百度信息流广告,将对接流程记录如下:
业务需求:通过用户点击广告, 获取用户信息,统计有用户信息
实现原理:
*.用户点击百度app中的广告
*.百度app记录手机用户点击事件,信息,并将用户信息传给百度服务器
*.百度服务器回调广告主事先在百度监控平台设置好的url(广告主自己开发)
*.广告主url获取用户点击事件,根据请求参数,来匹配用户的转换数据(应用激活)
*.广告主将匹配陈宫的转换数据拼接后
*.使用callback_rul,将数据回传到百度
*.结束
业务流程如下:
具体接口文档很清楚,就不多说:
接口 文档地址: http://ocpc.baidu.com/developer/ocpc-doc/app/app-interface/README1.html
回调地址文档:http://ocpc.baidu.com/developer/ocpc-doc/app/app-interface/README1.html#callbackUrl
需要注意地方:
1.百度推广后台回调监测 URL,即feedback url(由广告主自行开发)
2.广告主将匹配成功的转化数据与广告点击数据进行拼接,使用callback_url将数据回传给百度
3.广告主需要将url,在百度监控后台设置好
4.代码 中的okey需要与百度监控后台对应
百度监测中心:
设置url地方:工具中心->转换追踪->新建
百度推广回调广告主代码逻辑:
private static string Okey = "";
public AdChannelNotice Notify(ushort behaviorType, HttpRequest request)
{
var qs = request.Query;
var os = ParseOs(qs["os"]);
var idfa = qs["idfa"];
var imeiMd5 = qs["imei_md5"];
var timestamp = qs["ts"];
var id = qs["ip"];
var raw = request.QueryString.ToString();
return new AdChannelNotice(behaviorType, os, raw, TimestampHelper.FromTimestamp(timestamp))
{
Idfa = idfa,
AndroidId = "",
ImeiMd5 = imeiMd5,
};
}
private ushort ParseOs(string os)
{
switch (os)
{
case "1": return MobileOs.Ios;
case "2": return MobileOs.Android;
default: return 1;
}
}
广告主拼接数据后,将数据md5后给到百度广告主
代码逻辑:
public void Feedback(string raw)
{
var parameter = QueryHelpers.ParseQuery(raw);
if (parameter == null) return;
var callbackUrl = parameter["callback_url"];
var url = HttpUtility.UrlDecode(callbackUrl, Encoding.UTF8);
url = url.Replace("{{ATYPE}}", "activate");
url = url.Replace("{{AVALUE}}", "0");
var sign = ToMd5( $"&sign={url}{Okey}");
url = $"{url}{sign}";
HttpClient.GetAsync(url).Wait();
}
/// <summary>
/// 字符串加密Md5
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string ToMd5( string str)
{
StringBuilder sb = new StringBuilder(32);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x2"));
}
return sb.ToString();
}
private static class HttpUtility
{
/// <summary>
/// 对 URL 字符串进行编码
/// </summary>
/// <param name="str">字符串</param>
/// <returns>编码结果</returns>
public static string UrlEncode(string str, Encoding e)
{
return System.Web.HttpUtility.UrlEncode(str, e);
}
/// <summary>
/// 将已经为在 URL 中传输而编码的字符串转换为解码的字符串
/// </summary>
/// <param name="str">字符串</param>
/// <returns>解码结果</returns>
public static string UrlDecode(string str, Encoding e)
{
return System.Web.HttpUtility.UrlEncode(str, e);
}
}
原文地址:https://www.cnblogs.com/xiaohouye/p/11721319.html