Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save israelmartins96/0efaa1b1509e2baaa32ac7e6e6fbe324 to your computer and use it in GitHub Desktop.
Save israelmartins96/0efaa1b1509e2baaa32ac7e6e6fbe324 to your computer and use it in GitHub Desktop.
Popup Maker Advanced Targeting Conditions Display Popup for n Months After User Views n Pages
/**
* Requires Advanced Targeting Conditions extension: https://wppopupmaker.com/extensions/advanced-targeting-conditions/
*
* Sets a cookie to display the popup for the duration of the cookie lifetime, after the user has viewed n pages.
* Reset when the cookie expires.
*
* Popup ID: 1526 (REPLACE throughout snippet with your popup ID)
*
* For this snippet to work, make sure:
* 1. No cookie is set or linked to the trigger in the popup's Popup Settings > Triggers
* 2. The following conditions are set:
* - User Has Viewed X Pages
* - Cookie Exists (this should be the value of the popupDisplayCookie; in this case, popup-display-cookie)
*
* Advanced Targeting Conditions: User Conditions:
* https://docs.wppopupmaker.com/article/238-advanced-targeting-conditions-user-conditions
*
* Advanced Targeting Condition: Cookie Conditions:
* https://docs.wppopupmaker.com/article/237-advanced-targeting-conditions-cookie-conditions
*/
(function ($, document, undefined) {
/**
* The site's path
*/
const sitePath = '/';
/**
* The cookie the popup "Cookie Exists" condition will check for to display the popup.
*/
const popupDisplayCookie = 'popup-display-cookie';
/**
* The cookie that tracks the page views.
*/
const popupTrackViewsCookie = 'pum_popup_1526_page_views';
/** Sets cookie
* name string name of the cookie
* value string or int value of the cookie
* durationDays int number of days to set the cookie for
* path string the cookie path, e.g., "/my-page/"
*/
const setCookie = ( name, value, durationDays, path ) => {
const theDate = new Date();
theDate.setTime( theDate.getTime() + ( durationDays * 24 * 60 * 60 * 1000 ) );
let expires = 'expires=' + theDate.toUTCString();
document.cookie = name + '=' + value + ';' + expires + ';path=' + path;
};
/**
* Checks for cookie existence
* name string cookie name
*/
const getCookie = function ( name ) {
let value = '; ' + document.cookie;
let parts = value.split( '; ' + name + '=' );
if ( parts.length == 2 ) return parts.pop().split( ';' ).shift();
};
$( document )
.on( 'pumInit', function () {
// Set cookie if the popup exists and the cookie does not exist or has expired, and reset the cookie that tracks page views
if ( PUM.getPopup( 1526 ) ) {
if( ! getCookie( popupDisplayCookie ) ) {
PUM.preventOpen( 1526 );
// Set cookie for 60 days (two months)
setCookie( popupDisplayCookie, 'for-two-months', 60, sitePath );
// Unset page-views-tracking cookie
setCookie( popupTrackViewsCookie, 0, 0, sitePath );
// Reset page-views-tracking cookie
setCookie( popupTrackViewsCookie, 0, 1, sitePath );
}
}
} );
}(jQuery, document))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment