Adobe plug-in: p_fo (Page First Only)
The p_fo
plug-in is a utility that checks for the existence of a specific JavaScript object. If the object doesn’t exist, then the plug-in creates the object and returns true
. If the JavaScript object already exists on the page, then it returns false
. This plug-in is useful to run code exactly once on a page. Several other plug-ins rely on this code to work. This plug-in is unnecessary if you’re not worried about how many times code runs on a page, or if you don’t use any dependent plug-ins.
Install the plug-in using the Web SDK extension
Adobe offers an extension that allows you to use most commonly-used plug-ins with the Web SDK.
-
Log in to Adobe Experience Platform Data Collection using your AdobeID credentials.
-
Click Tags on the left, then click the desired tag property.
-
Click Extensions on the left, then click the Catalog tab
-
Locate and install the Common Web SDK Plugins extension.
-
Click Data Elements on the left, then click the desired data element.
-
Set the desired data element name with the following configuration:
- Extension: Common Web SDK Plugins
- Data Element:
p_fo
-
Save and publish the changes to the data element.
Install the plug-in manually implementing the Web SDK
This plug-in is not yet supported for use within a manual implementation of the Web SDK.
Install the plug-in using the Adobe Analytics extension
Adobe offers an extension that allows you to use most commonly-used plug-ins with Adobe Analytics.
-
Log in to Adobe Experience Platform Data Collection using your AdobeID credentials.
-
Click the desired tag property.
-
Go to the Extensions tab, then click on the Catalog button
-
Install and publish the Common Analytics Plugins extension
-
If you haven’t already, create a rule labeled “Initialize Plug-ins” with the following configuration:
- Condition: None
- Event: Core – Library Loaded (Page Top)
-
Add an action to the above rule with the following configuration:
- Extension: Common Analytics Plugins
- Action Type: Initialize p_fo
-
Save and publish the changes to the rule.
Install the plug-in using custom code editor
If you do not want to use the Common Analytics Plugins plug-in extension, you can use the custom code editor.
- Log in to Adobe Experience Platform Data Collection using your AdobeID credentials.
- Click on the desired property.
- Go to the Extensions tab, then click the Configure button under the Adobe Analytics extension.
- Expand the Configure tracking using custom code accordion, which reveals the Open Editor button.
- Open the custom code editor and paste the plug-in code provided below into the edit window.
- Save and publish the changes to the Analytics extension.
Install the plug-in using AppMeasurement
Copy and paste the following code anywhere in the AppMeasurement file after the Analytics tracking object is instantiated (using s_gi
). Preserving comments and version numbers of the code in your implementation helps Adobe with troubleshooting any potential issues.
/******************************************* BEGIN CODE TO DEPLOY *******************************************/
/* Adobe Consulting Plugin: p_fo (pageFirstOnly) v3.0 (Requires AppMeasurement) */
function p_fo(c){if("-v"===c)return{plugin:"p_fo",version:"3.0"};a:{if("undefined"!==typeof window.s_c_il){var a=0;for(var b;a<window.s_c_il.length;a++)if(b=window.s_c_il[a],b._c&&"s_c"===b._c){a=b;break a}}a=void 0}"undefined"!==typeof a&&(a.contextData.p_fo="3.0");window.__fo||(window.__fo={});if(window.__fo[c])return!1;window.__fo[c]={};return!0};
/******************************************** END CODE TO DEPLOY ********************************************/
Use the plug-in
The p_fo
function uses the following arguments:
- on (required, string): The name of the JavaScript object that the plug-in creates if the object doesn’t yet exist on the page.
If the object doesn’t yet exist, this function returns true
and creates the object. If the object already exists, this function returns false
.
Example Calls
Example #1
The following code will check for the existence of the “myobject” object within the page. If the “myobject” object doesn’t exist, then the code will create the “myobject” object and return the value of true. As a result, the code within the conditional statement (i.e. Console.log(‘hello’);) will run.
On the other hand, if the “myobject” object already exists when the p_fo call takes place, then the p_fo function will return the value of false and, thus, the conditional statement will be considered false. In this case, the code within the conditional statement will not run.
if(p_fo("myobject"))
{
console.log("hello");
}
NOTE: Every time a new page object/DOM loads (or the current page reloads), the object specified in the on argument will no longer exist and thus the p_fo plug-in will again return true the first time it runs after the page finishes loading.
Version History
3.0 (March 19, 2021)
- Added version number as context data.
2.0
- Point release (recompiled, smaller code size).
- Changed return value type from integer to boolean
1.0
- Initial release.