roshanoracle
8/21/2018 - 2:04 PM

Network N - BlueKai CoreTag Data Collection Code (meta keyword handling) : GTM Syntax

Automatically handles meta keywords

<!-- GOOGLE TAG MANAGER SYNTAX - ONLY USE IN GTM-->

<!-- Begin BlueKai Tag -->
<script type="text/javascript">
window.bk_async = function() {

	// ALLOW MULTIPLE CALLS/SINGLE PAGE APPS
	bk_allow_multiple_calls = true;
	bk_use_multiple_iframes = true;

  // <optional> PHINTS : Declare variables about this page (or user) if you have them. By default, BlueKai will send the URL, document.title and meta tags
	//bk_addPageCtx('page_type', 'article'); // Ensure the key name only uses the characters 'azAZ09_'
	//...etc...

  // Grab 'tags' dataLayer vars if available
	if({{tags}}){bk_addPageCtx('tags', {{tags}});}	

	// PHINTS : Additional URL-based phints
	bk_addPageCtx('url1', document.location.hostname + document.location.pathname);
	bk_addPageCtx('url2', document.location.hostname + document.location.pathname);
	bk_addPageCtx('url3', document.location.hostname + document.location.pathname);
	bk_addPageCtx('path1', document.location.pathname);
	bk_addPageCtx('path2', document.location.pathname);
	bk_addPageCtx('path3', document.location.pathname);

  // GRAB META DATA AUTOMATICALLY
  
	// FUNCTION : Grab meta tag data
	function getMetaTags() {

		var metaBk = {};

		metaBk.meta_all = [];
		metaBk.meta_kvp = {};

		var metas = document.getElementsByTagName('meta');

		for (var i = 0; i < metas.length; i++) {

			if (metas[i].getAttribute("name")) {

				var keyName = metas[i].getAttribute("name");
				var keyValue = metas[i].getAttribute("content");

				// Declare KVP
				metaBk.meta_kvp[keyName] = keyValue;
				metaBk.meta_all.push(keyValue);

			} else if (metas[i].getAttribute("property")) {

				var keyName = metas[i].getAttribute("property");
				var keyValue = metas[i].getAttribute("content");

				// Declare KVP
				metaBk.meta_kvp[keyName] = keyValue;
				metaBk.meta_all.push(keyValue);

			}

		}

		return metaBk;
	}

	// FUNCTION : Declare meta tag data
	function declareBkMeta(data, separateMeta) {

		// Declare "meta_all" phint
		var meta_all = "|" + data.meta_all.join("|") + "|";
		bk_addPageCtx('meta_all', meta_all);

		// Declare meta tags separately
		if (separateMeta) {
			for (metaTag in data.meta_kvp) {

				var bkFriendlyName = metaTag.replace(/[^0-9,a-zA-Z_]+/g, "_");
				bk_addPageCtx("meta_" + bkFriendlyName, data.meta_kvp[metaTag]);

			}
		}
	}

	// Declare Meta Tags to BlueKai 
	declareBkMeta(getMetaTags(), false); // change 'false' to 'true' to sent meta tags as separate phints 

	// Send Data 
	BKTAG.doTag("ENTER CONTAINER ID HERE", 4); // Please generate a container in the DMP UI per website
};
(function() {
	var scripts = document.getElementsByTagName('script')[0];
	var s = document.createElement('script');
	s.async = true;
	s.src = "//tags.bkrtx.com/js/bk-coretag.js";
	scripts.parentNode.insertBefore(s, scripts);
}());
</script>
<!-- End BlueKai Tag -->