Skip to content

Instantly share code, notes, and snippets.

@robisatthefunction
Last active May 1, 2023 18:17
Show Gist options
  • Save robisatthefunction/24c6546a18f44c7a81d3fbc99ec97933 to your computer and use it in GitHub Desktop.
Save robisatthefunction/24c6546a18f44c7a81d3fbc99ec97933 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
var optlyPages = window.optimizelyEdge.get("state").getActivePages();
// Implement the following method
function activateOptlyPage(optlyPage) {
var _apiName = optlyPage.apiName;
//Force deactivation of the page
window.optimizelyEdge.push({
type: "page",
pageName: optlyPages[optlyPage].apiName,
isActive: false,
});
//Reactivate the page. This will force Optimizely to evaluate the conditions again as if reloading the DOM.
window.optimizelyEdge.push({
type: "page",
pageName: optlyPages[optlyPage].apiName,
});
// console.log('Optimizely Reactivated page - ' + optlyPages[optlyPage].apiName)
}
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// loop through all pages and force deactivation and then reactivate
for (var page in optlyPages) {
activateOptlyPage(page);
//console.log('Optimizely activating page - ' + page)
}
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment