1

I want to add Segment to my WordPress site. I read the best way to do this is to edit functions.php and use wp_enqueue_scripts

I've replaced my tracking code with "test"

This is the segment code:

<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>
0

add that code to a js in your theme folder (or child theme if you're using it). for this example I named the script "analytics.js" and put it in a directory called "js" in my child theme folder.

Now register and enqueue the file by placing this code in your functions.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' );
  • I'm doing something wrong. I created a file on sublime, added the code and called it segment.js. I then added the file to the child theme directory under a new folder called js. I added your code to functions.php and changed the script name to segment.js – Prateek Apr 14 '17 at 18:56
  • Make sure that you don't have <script> at the top or </script> at the bottom? – rudtek Apr 14 '17 at 19:00
  • I did have that at the top and the bottom. I removed it function wpse_load_script() { // Register the script like this: wp_register_script( 'analytics-script', get_stylesheet_directory_uri() . '/js/segment.js', array( 'jquery' ) ); // You can then enqueue the script: wp_enqueue_script( 'analytics-script' ); } add_action( 'wp_enqueue_scripts', 'wpse_load_script' ); – Prateek Apr 14 '17 at 19:06
  • You removed it from the .js file, right? Is the script being found when you debug or are you getting a different error. – rudtek Apr 14 '17 at 19:11
  • Yes, I removed it from the .js file - pastebin.com/BDjFeZXV Nothing is showing up in debugger and it is saying no recent data – Prateek Apr 14 '17 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.