通过 Adjust Web SDK,您可以在网页应用中记录归因、事件以及更多数据。该指南展示了如何将该 SDK 集成至应用。
1. 添加 SDK 至您的项目
要开始设置,请将 SDK 添加至网页应用。Adjust SDK 可在 CommonJS 和 AMD (Asynchronous Module Definition) 环境中运行。通过 CDN (内容分发网络) 加载时,您可以通过全局Adjust
命名空间对其进行访问。
使用 CDN
通过 CDN 加载 SDK 时,您应该在生产环境中使用压缩版本。
- 您可以通过添加一个版本至 CDN 目标来加载指定版本,如下所示:
https://cdn.adjust.com/adjust-5.8.0.min.js
。 - 要加载最新版本,请使用
adjust-latest
包,如下所示:https://cdn.adjust.com/adjust-latest.min.js
。该包会自动更新,您无需更改目标文件。
要通过 CDN 加载 SDK,请将以下代码片段加入网页应用的<head>
元素中:
<script type="application/javascript">!function(e,t,r,a,n,o,l,i,s,c,d,h,u,f,m){var p=l+"_q",g=l+"_c";e[l]=e[l]||{},e[p]=e[p]||[],e[g]=e[g]||[];for(let t=0;t<i.length;t++)h(e[l],e[p],i[t]);for(let t=0;t<s.length;t++)u(e[l],e[p],s[t]);for(let t=0;t<c.length;t++){const r=c[t][0],a=c[t][1];let n;e[l][r]=function(...t){return n=this,e[g].push((function(){n[d]=new e[l][r](...t)})),n};for(let t=0;t<a.length;t++){const o=a[t];e[l][r].prototype[o]=function(...t){e[g].push((function(){n[d][o](...t)}))}}}f=t.createElement(r),m=t.getElementsByTagName(r)[0],f.async=!0,f.src=a,f.onload=function(){for(var t=0;t<e[g].length;t++)e[g][t]();e[g]=[];for(t=0;t<e[p].length;t++)if(e[p][t][1][0]&&e[p][t][1][0][d])e[l][e[p][t][0]](e[p][t][1][0][d]);else{const r=e[p][t][2];r?e[l][e[p][t][0]].apply(e[l],e[p][t][1]).then((e=>r.resolve(e))):e[l][e[p][t][0]].apply(e[l],e[p][t][1])}e[p]=[]},m.parentNode.insertBefore(f,m)}(window,document,"script","https://cdn.adjust.com/adjust-latest.min.js",0,0,"Adjust",["initSdk","getAttribution","getWebUUID","setReferrer","trackEvent","addGlobalCallbackParameters","addGlobalPartnerParameters","removeGlobalCallbackParameter","removeGlobalPartnerParameter","clearGlobalCallbackParameters","clearGlobalPartnerParameters","switchToOfflineMode","switchBackToOnlineMode","stop","restart","gdprForgetMe","disableThirdPartySharing","trackThirdPartySharing","initSmartBanner","showSmartBanner","hideSmartBanner"],["waitForAttribution","waitForWebUUID"],[["ThirdPartySharing",["addGranularOption","addPartnerSharingSetting"]]],"__realObj",(function(e,t,r){e[r]=function(){t.push([r,arguments])}}),(function(e,t,r){e[r]=function(){const e={};return e.promise=new Promise(((t,r)=>{e.resolve=t,e.reject=r})),t.push([r,arguments,e]),e.promise}}));</script>
Adjust SDK 会在每个页面上加载并初始化。
子资源完整性
请使用子资源完整性检查来抵御 XSS (跨站脚本) 攻击。要在运行包之前对其进行验证,请使用以下代码片段 (请注意额外的crossOrigin
、 integrity
和经过编码的哈希值):
<script type="application/javascript">!function(e,t,r,n,a,o,i,l,s,c,d,h,u,f,m){var p=i+"_q",g=i+"_c";e[i]=e[i]||{},e[p]=e[p]||[],e[g]=e[g]||[];for(let t=0;t<l.length;t++)h(e[i],e[p],l[t]);for(let t=0;t<s.length;t++)u(e[i],e[p],s[t]);for(let t=0;t<c.length;t++){const r=c[t][0],n=c[t][1];let a;e[i][r]=function(...t){return a=this,e[g].push((function(){a[d]=new e[i][r](...t)})),a};for(let t=0;t<n.length;t++){const o=n[t];e[i][r].prototype[o]=function(...t){e[g].push((function(){a[d][o](...t)}))}}}f=t.createElement(r),m=t.getElementsByTagName(r)[0],f.async=!0,f.src=n,o&&(f.crossOrigin="anonymous",f.integrity=o),f.onload=function(){for(var t=0;t<e[g].length;t++)e[g][t]();e[g]=[];for(t=0;t<e[p].length;t++)if(e[p][t][1][0]&&e[p][t][1][0][d])e[i][e[p][t][0]](e[p][t][1][0][d]);else{const r=e[p][t][2];r?e[i][e[p][t][0]].apply(e[i],e[p][t][1]).then((e=>r.resolve(e))):e[i][e[p][t][0]].apply(e[i],e[p][t][1])}e[p]=[]},m.parentNode.insertBefore(f,m)}(window,document,"script","https://cdn.adjust.com/adjust-5.8.0.min.js",0,"sha384-YR61L/f7VPKFN/TX5B8GpDD7MJlerBFDvNVnKIp7ac2uwHHlP0O9mdOw5aDZ4T/6","Adjust",["initSdk","getAttribution","getWebUUID","setReferrer","trackEvent","addGlobalCallbackParameters","addGlobalPartnerParameters","removeGlobalCallbackParameter","removeGlobalPartnerParameter","clearGlobalCallbackParameters","clearGlobalPartnerParameters","switchToOfflineMode","switchBackToOnlineMode","stop","restart","gdprForgetMe","disableThirdPartySharing","trackThirdPartySharing","initSmartBanner","showSmartBanner","hideSmartBanner"],["waitForAttribution","waitForWebUUID"],[["ThirdPartySharing",["addGranularOption","addPartnerSharingSetting"]]],"__realObj",(function(e,t,r){e[r]=function(){t.push([r,arguments])}}),(function(e,t,r){e[r]=function(){const e={};return e.promise=new Promise(((t,r)=>{e.resolve=t,e.reject=r})),t.push([r,arguments,e]),e.promise}}));</script>
使用 npm
Adjust SDK 还可用于 npm。要将包加入您的项目,请使用您常用的包管理器:
2. 初始化 SDK
安装 SDK 后,需要进行初始化。为此,请调用initSdk
方法。您可以使用该方法中的参数来自定义 SDK 在应用中的工作方式。
要初始化 SDK,您必须将以下参数添加至initSdk
调用:
appToken
(string
):您的 Adjust 应用识别码。environment
(string
):SDK 运行的环境。- 传递
sandbox
来在测试模式下运行 SDK。 - 传递
production
来在生产模式下运行 SDK,方便发布。
- 传递
Adjust.initSdk({ appToken: "YOUR_APP_TOKEN", environment: "sandbox",});