{"id":219,"date":"2007-07-17T10:33:34","date_gmt":"2007-07-17T10:33:34","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2007\/07\/dynamically_setting_domain_names_for_webtrends-shtml\/"},"modified":"2007-07-17T10:33:34","modified_gmt":"2007-07-17T10:33:34","slug":"dynamically_setting_domain_names_for_webtrends-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/","title":{"rendered":"Dynamically Setting Domain Names For Webtrends"},"content":{"rendered":"<p>\nWe use <a href=\"http:\/\/www.webtrends.com\/\">Webtrends<\/a> at work to track usage of our websites.\n<\/p>\n<p>\nWe use the on demand version which relies on embedding a JavaScript tag onto the page. Recently, we&#8217;ve moved to using 1st party cookies (cookies set by the site) instead of 3rd party cookies (cookies set externally by webtrends). These are set in the JavaScript tag and are hard coded to the site, a real pain if you happen to move domain names as you need to code them up specifically each time. We got caught out recently by using a tag with the wrong domain name set in it, meaning we were unable to track the page impressions. We needed a solution&#8230;\n<\/p>\n<p>\nAs the tag is being set in JavaScript, we can use JavaScript to set the domain dynamically. Not all our sites are served dynamically so using JavaScript should mean it would work for anyone using this tag.\n<\/p>\n<p>\nHere&#8217;s the offending bit of code&#8230;\n<\/p>\n<div class=\"code\"><code>\/\/ Code section for Set the First-Party Cookie domain<br \/>\nvar gFpcDom=\".kerrang.com\";<br \/>\n<\/code><\/div>\n<p>\nThis works for anything served from a <a href=\"http:\/\/www.kerrang.com\/\">Kerrang.com<\/a> domain. However, what if we want to use a different domain for some reason?\n<\/p>\n<p>\nWe&#8217;ll JavaScript has a property in the <code>document<\/code> object called <code>domain<\/code>, we can use this at runtime to find out the full domain name the page was served from.\n<\/p>\n<p>\nHowever, we don&#8217;t need the full domain name, just parent domain. For example, <var>www.kerrang.com<\/var> should become <var>kerrang.com<\/var>.\n<\/p>\n<p>\nWe can fix this by using several approaches, but the easiest is the split, splice, join method.\n<\/p>\n<p>\nFirstly we split the domain into it&#8217;s components by splitting on the periods.\n<\/p>\n<div class=\"code\"><code>document.domain.split('.');<br \/>\n<\/code><\/div>\n<p>\nThis gives us the array <var>(&#8220;www&#8221;,&#8221;kerrang&#8221;,&#8221;com&#8221;)<\/var>.\n<\/p>\n<p>\nSecondly we splice this to remove the first element.\n<\/p>\n<div class=\"code\"><code>document.domain.split('.').splice(1.3);<br \/>\n<\/code><\/div>\n<p>\nThis is saying to take 3 elements after the 1st element in the array. This should cover us if we want to use something like kerrang.co.uk. So after running this we need to should have an array looking like this, <var>(&#8220;kerrang&#8221;,&#8221;com&#8221;)<\/var>.\n<\/p>\n<p>\nFinally we join this back together again, making sure a period is between each joined element.\n<\/p>\n<div class=\"code\"><code>document.domain.split('.').splice(1,3).join('.');<br \/>\n<\/code><\/div>\n<p>\nRunning this should give us <var>kerrang.com<\/var>, which is acceptable to use in the Webtrends tag.\n<\/p>\n<p>\nSo our final code looks like this&#8230;\n<\/p>\n<p><div class=\"code\"><code>var gFpcDom = document.domain.split('.').splice(1,3).join('.');<br \/>\n<\/code><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We use Webtrends at work to track usage of our websites. We use the on demand version which relies on embedding a JavaScript tag onto the page. Recently, we&#8217;ve moved to using 1st party cookies (cookies set by the site) instead of 3rd party cookies (cookies set externally by webtrends). These are set in the &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Dynamically Setting Domain Names For Webtrends&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,78],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-work"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Dynamically Setting Domain Names For Webtrends - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dynamically Setting Domain Names For Webtrends - Robert Price\" \/>\n<meta property=\"og:description\" content=\"We use Webtrends at work to track usage of our websites. We use the on demand version which relies on embedding a JavaScript tag onto the page. Recently, we&#8217;ve moved to using 1st party cookies (cookies set by the site) instead of 3rd party cookies (cookies set externally by webtrends). These are set in the &hellip; Continue reading &quot;Dynamically Setting Domain Names For Webtrends&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2007-07-17T10:33:34+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Dynamically Setting Domain Names For Webtrends\",\"datePublished\":\"2007-07-17T10:33:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/\"},\"wordCount\":367,\"keywords\":[\"JavaScript\",\"Work\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/\",\"name\":\"Dynamically Setting Domain Names For Webtrends - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2007-07-17T10:33:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/dynamically_setting_domain_names_for_webtrends-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dynamically Setting Domain Names For Webtrends\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dynamically Setting Domain Names For Webtrends - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Dynamically Setting Domain Names For Webtrends - Robert Price","og_description":"We use Webtrends at work to track usage of our websites. We use the on demand version which relies on embedding a JavaScript tag onto the page. Recently, we&#8217;ve moved to using 1st party cookies (cookies set by the site) instead of 3rd party cookies (cookies set externally by webtrends). These are set in the &hellip; Continue reading \"Dynamically Setting Domain Names For Webtrends\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/","og_site_name":"Robert Price","article_published_time":"2007-07-17T10:33:34+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Dynamically Setting Domain Names For Webtrends","datePublished":"2007-07-17T10:33:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/"},"wordCount":367,"keywords":["JavaScript","Work"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/","name":"Dynamically Setting Domain Names For Webtrends - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2007-07-17T10:33:34+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/dynamically_setting_domain_names_for_webtrends-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Dynamically Setting Domain Names For Webtrends"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}