wordpress 数据库

Managing your data layer is key for your success. In this post, we will review how to handle that using the popular WordPress CMS.

管理数据层是成功的关键。 在这篇文章中,我们将回顾如何使用流行的WordPress CMS处理该问题。

A common way to manage code in WordPress is through plugins, where standard functions allow you to retrieve key values from your CMS. A reusable code that you can save in your plugin directory is provided at the end of this post.

在WordPress中管理代码的一种常见方式是通过插件,其中的标准功能可让您从CMS中检索键值。 本文结尾处提供了可保存在插件目录中的可重用代码。

The key values that you mostly need are:

您最需要的关键值是:

- Page content attributes (page name, category, page type, locale…)

-页面内容属性(页面名称,类别,页面类型,区域设置…)

- User information (login id, login status, user role…)

-用户信息(登录ID,登录状态,用户角色…)

- System information (WordPress version, Theme name and version, encoding…)

-系统信息(WordPress版本,主题名称和版本,编码…)

Now, you can start building your data layer.

现在,您可以开始构建数据层。

(Cleaning your values)

Your goal is to write values that are as clean as possible, because any mess written on the page will have to be cleaned up at some point. For instance, if your “login status” value should be “logged in”, make sure you write this directly and not a value like “1” or “false”. In practice:

您的目标是编写尽可能干净的值,因为写在页面上的任何混乱都必须在某个时候清除。 例如,如果您的“登录状态”值应为“已登录”,请确保您直接编写此值,而不是诸如“ 1”或“ false”之类的值。 在实践中:

- if you are a developer, present your JSON output to the team in charge of the data collection; keep your raw values rather than make up your own value and request feedback/confirmation.

-如果您是开发人员,请向负责数据收集的团队提供JSON输出; 保留您的原始价值,而不是弥补自己的价值并要求反馈/确认。

- If you are a business user or in charge of business requirements and specifications, provide as many details. For instance, if you the user is not logged in, the login status should be “logged out” and other values like user name, id and role should be left empty. This is not trivial, because if you don’t specify it, you can get values like “0” or “false” whereas you rely on specific strings for your rules and your Analytics reports.

-如果您是企业用户或负责企业要求和规范,请提供尽可能多的详细信息。 例如,如果用户未登录,则登录状态应为“注销”,而其他值(如用户名,id和角色)应保留为空。 这并非易事,因为如果不指定它,则可以获取诸如“ 0”或“ false”之类的值,而您的规则和Google Analytics(分析)报告则依赖于特定的字符串。

As part of your governance, maintain and enforce your specifications since small details can impact your data collection and delay projects because of unnecessary workarounds and fixes to be added through your tag management system.

作为治理的一部分,维护和执行规范是因为小细节可能会影响数据收集并延迟项目,因为不必要的变通办法和要通过标签管理系统添加的修复程序。

For instance, if you have integer values (usually IDs), make sure they are written as strings; this is why I am using the strval function in my code. If you have URL values, make sure you remove escape “\”; this is the reason why I am using JSON_UNESCAPED_SLASHES in my json_encode function.

例如,如果您有整数值(通常是ID),请确保将它们写为字符串;否则,请使用整数。 这就是为什么我在代码中使用strval函数的原因。 如果您具有URL值,请确保删除转义符“ \”; 这就是为什么我在json_encode函数中使用JSON_UNESCAPED_SLASHES的原因。

(Adding values to your schema)

While you can still use the W3C digitalData object, the new Adobe Data Layer, documented at https:///adobe/adobe-client-data-layer is recommended, in particular for new implementation methods like Adobe Experience Platform Web SDK. This data layer is Event-Driven and using XDM means you will have streamlined data collection. Note that as best practices are consolidating around XDM schemas, you may have to adjust your schema when updating to our latest data collection using the Web SDK.

虽然您仍然可以使用W3C digitalData对象,但是建议使用https:///adobe/adobe-adobe-client-data-layer记录的新Adobe Data Layer,特别是对于Adobe Experience Platform Web SDK等新的实现方法而言。 该数据层是事件驱动的,使用XDM意味着您将简化数据收集。 请注意,由于最佳实践正在围绕XDM模式进行整合,因此在使用Web SDK更新到我们的最新数据收集时,可能必须调整模式。

In the code sample provided, the two options are present because you may have existing Data Elements and rules using the digitalData object; having the new data layer with an XDM schema means that you can transition to the Experience Edge data collection at a later stage.

在提供的代码示例中,提供了两个选项,因为使用digitalData对象可能已有数据元素和规则。 使新数据层具有XDM架构意味着您可以在稍后阶段过渡到Experience Edge数据集合。

There are some values specific to WordPress that you will want to collect: the Wordpress version, theme and them version. These values will help you maintain your WordPress setup and assess issues on data caused by updated; also, if you manage multiple WordPress instances, this will help you assess that all instances are correctly maintained, for security and performance.

您需要收集一些特定于WordPress的值:Wordpress版本,主题及其版本。 这些值将帮助您维护WordPress设置并评估由更新引起的数据问题; 同样,如果您管理多个WordPress实例,则可以帮助您评估所有实例是否都正确维护,以确保安全性和性能。

(Getting along with your data layer)

Your data layer is obviously meant to be consumed and as you are writing it on your page, let’s make sure you also include some of our key recommended header values and scripts:

显然,您的数据层是要消耗的,并且在将其写入页面时,请确保还包括一些我们推荐的关键头值和脚本:

- DNS prefetch

-DNS预取

- Launch monitoring

-启动监控

- Adobe Target pre-hiding

-Adobe Target预隐藏

- Launch script

-启动脚本

Once your file is ready, simply save it in your plugin directory and activate it.

准备好文件后,只需将其保存在插件目录中并激活即可。

<?php
/**
 * Plugin Name: Adobe Data Layer
 * Plugin URI: -redacted-
 * Description: Deploy the Adobe Data Layer
 * Author: JB Creusat
 * Version: 1.0
 */
function adobe_data_layer() {
 $user = wp_get_current_user();
$myObj->serverName = $_SERVER['SERVER_NAME'];
$myObj->serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$myObj->phpVersion = phpversion();
$myObj->userID = get_current_user_id();
$myObj->loginStatus = is_user_logged_in(); 
$myObj->userName = $user->user_login;
$myObj->userRole = $user->roles[0];
$myObj->siteName = get_bloginfo( 'name' );
$myObj->siteDescription = get_bloginfo( 'description' );
$myObj->siteDescription = get_bloginfo( 'description' );
$myObj->siteCharset = get_bloginfo( 'charset' );
$myObj->siteWpVersion = get_bloginfo( 'version' );
$myObj->siteLanguage = get_bloginfo( 'language' );
$myObj->authorName = get_author_name();
$myObj->templateTheme = wp_get_theme()->get( 'Name' );
$myObj->templateVersion = wp_get_theme()->get( 'Version' );
$myObj->locale = get_locale();
$myObj->pageID = get_the_ID();
$myObj->pageCategory = get_the_category($myObj->pageID)[0]->slug;
$myObj->pageTitle = get_post($myObj->pageID)->post_title;
$myObj->pageName = get_post($myObj->pageID)->post_name;
$myObj->pageType = get_post($myObj->pageID)->post_type;
$myObj->pagePermalink = get_permalink($myObj->pageID);
$myObj->pageURI = get_page_uri($myObj->pageID);
if ($myObj->pageCategory) {$myObj->pageNameFormatted = $myObj->pageCategory.":".$myObj->pageName;}
else {$myObj->pageNameFormatted = $myObj->pageName;}
if (get_search_query()) {$myObj->searchQuery = get_search_query();}
$myJSON = json_encode($myObj, JSON_UNESCAPED_SLASHES);

?>
<link rel="dns-prefetch" href="//track.creusat.com">
 <link rel="dns-prefetch" href="//dpm.demdex.net">
 <link rel="dns-prefetch" href="//fast.creusat.demdex.net">
 <link rel="dns-prefetch" href="//pixel.everesttech.net">
 <link rel="dns-prefetch" href="//cm.everesttechnet">
 <link rel="dns-prefetch" href="//creusatcom.tt.omtrdc.net">
<script>
 window._satellite = window._satellite || {};
 window._satellite._monitors = window._satellite._monitors || [];
 window._satellite._monitors.push({
 ruleTriggered: function (event) {
 console.log(
 'rule triggered', 
 event.rule
 );
 },
 ruleCompleted: function (event) {
 console.log(
 'rule completed', 
 event.rule
 );
 },
 ruleConditionFailed: function (event) {
 console.log(
 'rule condition failed', 
 event.rule, 
 event.condition
 );
 }
 });
 </script>
<script type="text/javascript">
 ;(function(win, doc, style, timeout) {
 var STYLE_ID = 'at-body-style';
 function getParent() {
 return doc.getElementsByTagName('head')[0];
 }
 function addStyle(parent, id, def) {
 if (!parent) {
 return;
 }
 var style = doc.createElement('style');
  = id;
 style.innerHTML = def;
 parent.appendChild(style);
 }
 function removeStyle(parent, id) {
 if (!parent) {
 return;
 }
 var style = doc.getElementById(id);
 if (!style) {
 return;
 }
 parent.removeChild(style);
 }
 addStyle(getParent(), STYLE_ID, style);
 setTimeout(function() {
 removeStyle(getParent(), STYLE_ID);
 }, timeout);
 }(window, document, "body {opacity: 0 !important}", 3000));
</script>
<script data-cfasync="false" data-pagespeed-no-defer type="text/javascript">
 window.pageValue = <?php echo $myJSON; ?>
</script>
<script>
<! - Legacy Data Layer →
 digitalData = window.digitalData|| {};
 digitalData.page = {
 pageInfo: {
 pageName: pageValue.pageNameFormatted,
 language: pageValue.siteLanguage
 }
 };
 digitalData.page.category= {
 pageType: pageValue.pageType,
 primaryCategory: pageValue.pageCategory,
 };
 digitalData.search= {
 keyword: pageValue.searchQuery
 };
 digitalData.user = {
 id: pageValue.userID,
 status: pageValue.loginStatus,
 type: pageValue.userRole
 }
<! - End - Legacy Data Layer →

 window.adobeDataLayer = window.adobeDataLayer || [];
 window.adobeDataLayer.push({
 "event": "eddlPageLoad",
 "xdm": {
 "web": {
 "webPageDetails": {
 "name": pageValue.pageNameFormatted,
 "server": pageValue.serverName
 }
 },
 "user": {
 "userID": pageValue.userID,
 "loginStatus": pageValue.loginStatus,
 "userName": pageValue.userName,
 "userRole": pageValue.userRole 
 }
 } 
 });
</script>
<?php
 } 
add_action( 'wp_head', 'adobe_data_layer', 8, 3);
add_action( 'wp_enqueue_scripts', 'my_custom_script_load', 9, 3);
function my_custom_script_load(){
 wp_register_script('adobe-client-data-layer',plugin_dir_url( __FILE__ ).'scripts/adobe-client-data-layer.min.js');
 wp_register_script('adobe-launch',plugin_dir_url( __FILE__ ).'scripts/ld/AAAAAAAAAA/BBBBBBBBBB/launch-ccccccccccc.min.js', array ('jquery-core'));
wp_enqueue_script('adobe-client-data-layer');
 wp_enqueue_script('adobe-launch');
}
// add async and defer attributes to enqueued scripts
function add_async_defer($tag, $handle, $src) {
 if ($handle === 'adobe-client-data-layer') {
 $tag = '<script src="' . esc_url( $src ) . '" async defer></script>';
 }
 return $tag;
}
add_filter('script_loader_tag', 'add_async_defer', 10, 3);
function add_async($tag, $handle, $src) {
 if ($handle === 'adobe-launch') {
 $tag = '<script src="' . esc_url( $src ) . '" async></script>';
 }
 return $tag;
}
add_filter('script_loader_tag', 'add_async', 10, 3);

翻译自: https:///@jbcreusat/managing-your-data-layer-in-wordpress-97f86baaf5e2

wordpress 数据库