我想向我的WordPress站点添加段。我认为最好的方法是编辑函数。php并使用wp\\u enqueue\\u脚本
我已将跟踪代码替换为“测试”
这是段代码:
<script>
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";
analytics.load("TEST");
analytics.page();
}}();
</script>
SO网友:rudtek
将该代码添加到主题文件夹(或子主题,如果您正在使用它)中的js中。对于这个示例,我将脚本命名为“analytics.js”,并将其放在子主题文件夹中名为“js”的目录中。
现在,通过在函数中放置此代码来注册文件并将其排队。php
function wpse_load_script()
{
// Register the script like this:
wp_register_script( \'analytics-script\', get_stylesheet_directory_uri() . \'/js/analytics.js\', array( \'jquery\' ) );
// You can then enqueue the script:
wp_enqueue_script( \'analytics-script\' );
}
add_action( \'wp_enqueue_scripts\', \'wpse_load_script\' );