# Have Pinegrow interaction run only on first visit

**URL:** <https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713>\
**Category:** Javascript\
**Created:** [November 23, 2020, 1:12pm UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713 "2020-11-23T13:12:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowfeald](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@snowfeald](https://forum.pinegrow.com/u/snowfeald)\
**Post date:** [November 23, 2020, 1:12pm UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/1 "2020-11-23T13:12:10Z")

</div>

As the title suggests, I’m looking to use some kind of caching so that the javascript wont run after the first visit.

I have an animation that runs on the landing page that obscures all other content for two seconds~. It’s nice for the first but if people want to come back to homepage, refresh etc. it’s obviously annoying.

Not really sure how to approach this, perhaps the animation parent div could be deleted and cached to be gone as it no longer serves a purpose? Keen to have feedback on this.

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [November 23, 2020, 2:26pm UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/2 "2020-11-23T14:26:10Z")

</div>

I’m not an expert on Interactions, but here is how I would do it.  
You have a couple of choices, depending on how long you want the block to re-running the animation to exist and what your privacy policies are currently.  
For long-term blocking you’ll want to add a cookie value with a stated expiration. This will persist even if the visitor quits the browser. When the user refreshes the page you use JS to fetch the cookie value and then change some classes on the page. Those classes would be incorporated into your Interactions target values.  
For shorter term blocking, you can either use session storage (only persists as long as the visitor doesn’t close the current browser tab) or local storage (persists as long as the visitor doesn’t quit the browser) and do the same thing.  
Not being an interactions expert, I don’t have a good feeling for whether you it would be easy to add the variable check on some main script and I’m not by my computer to check. If nobody else responds I can try to check this a little later.

Hope this helps.  
Bob

> **[Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)**
>
> The Web Storage API provides mechanisms by which browsers can store key/value pairs, in a much more intuitive fashion than using cookies.

  

> **[Window.sessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage)**
>
> The read-only sessionStorage property accesses a session Storage object for the current origin. sessionStorage is similar to localStorage; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the...

  

> **[Window.localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)**
>
> The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.

---

<div class="post-metadata">

**Author:** ![snowfeald](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@snowfeald](https://forum.pinegrow.com/u/snowfeald)\
**Post date:** [November 25, 2020, 2:06am UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/3 "2020-11-25T02:06:02Z")

</div>

Thanks for directing me to the relevant resources Rob.

I toyed around with sessionStorage to save the state of the preload div classes because the final step of the interaction is to add a class with the css property of display: hidden. The other option I looked at was having something like `document.getElementsByClassName("removeclass")[0].remove();` where the preload div is removed once ‘removeclass’ is added to the preload div at the end of the interaction.

Unfortunately I’m an absolute javascript novice so I’ve been stuck looking through stackoverflow solutions and blog posts to try and figure something out. I’ll keep playing with this when I have the time but if anyone feels like lending a helping hand, it would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [November 25, 2020, 10:24am UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/4 "2020-11-25T10:24:35Z")

</div>

@snowfeald I’m happy to write you some code if you give me a little better overview of the project. Right now not sure what is being animated/hidden/revealed.  
Cheers,  
Bob

---

<div class="post-metadata">

**Author:** ![snowfeald](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@snowfeald](https://forum.pinegrow.com/u/snowfeald)\
**Post date:** [November 25, 2020, 12:26pm UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/5 "2020-11-25T12:26:09Z")

</div>

Wow, thanks Rob!

Basically I have a page structured like this: [https://codepen.io/snowfealds/pen/WNGeyON](https://codepen.io/snowfealds/pen/WNGeyON) where the preload div covers all other content, a pinegrow interaction is set to run when the page finishes loading, and when that animation is completed the “preload-hold” is hidden so that the user can see the actual site content.

Whenever someone reloads the homepage (the only page the preloader is on) / comes back to the page etc. they have to view the animation again. So the goal is to use some kind of cookies to hide it after the first visit. sessionStorage seems appropriate for this as I don’t mind them viewing it again if they return at a later time.

[https://github.com/js-cookie/js-cookie](https://github.com/js-cookie/js-cookie) I came across this which could be good, though I haven’t had tiome to do more research since last writing…

Thanks once again!!

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [November 25, 2020, 1:43pm UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/6 "2020-11-25T13:43:19Z")

</div>

@snowfeald Here you go. Drag this script to the insert code box and then drag the resulting element below the insert code box to the end of the body, or add it using your favorite code editor.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/e/ec9b73e02a48e14f614c3a08c544386a2659f879.png)  
You can remove the comments for production, I just put them into explain what each step was doing. I might also customize the session variable name be prefixing it to make it unique. Likely no other site is using “isNotFirstVisit”, but you never know! After adding you may need to refresh the page.

```auto
<script>
//While page is loading check if there is a session variable set for isNotFirstVisit - will be null if this is the first visit
let isNotFirstVisit = sessionStorage.getItem("isNotFirstVisit");
//check if isNotFirstVisit = null (or false)
if(!isNotFirstVisit) {
    //if this is the first visit, let the animation play and set the variable to "true"
    sessionStorage.setItem("isNotFirstVisit", true);
} else {
    //if this is not the first visit then find the pre-loader - this assumes that the first item with the class name "preload-hold" is the one you want hidden
    let loader = document.getElementsByClassName("preload-hold")[0];
    //set the visibility to hidden
    loader.style.visibility = "hidden";
    //set the display to none
    loader.style.display ="none";
}
</script>

Good Luck and keep me posted!
Bob
```

---

<div class="post-metadata">

**Author:** ![snowfeald](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@snowfeald](https://forum.pinegrow.com/u/snowfeald)\
**Post date:** [November 26, 2020, 12:13am UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/7 "2020-11-26T00:13:35Z")

</div>

Hi Rob, this works perfectly! On a live site going from one page back to the homepage, the preloader is gone straight away; this is exactly what I wanted! I’d love to send you some coin for this if you wouldn’t mind?

Cheers,

---

<div class="post-metadata">

**Author:** ![red-rosefields](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/red-rosefields/32/5392_2.png) [@red-rosefields](https://forum.pinegrow.com/u/red-rosefields)\
**Post date:** [December 5, 2020, 8:56pm UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/8 "2020-12-05T20:56:02Z")

</div>

My case is similar, I am too looking for one of my animations not to repeat after the first session (page load) just like @snowfeald but the thing is I can’t hide or display “none” the element (like your script above) as there are some more interactions in that very same element and they need to keep working after the first load.

So I was trying to use the functionality, **Trigger condition** \> **has a class** on the specific interaction. Then by removing that class after the first-page load, the animation would not trigger as js deleted that class. Hope it makes sense.  
I am trying to manipulate your script but js is out of my skills.

Any hint would be really appreciated.

`<script> //While page is loading check if there is a session variable set for isNotFirstVisit - will be null if this is the first visit let isNotFirstVisit = sessionStorage.getItem("isNotFirstVisit"); //check if isNotFirstVisit = null (or false) if(!isNotFirstVisit) { //if this is the first visit, let the animation play and set the variable to "true" sessionStorage.setItem("isNotFirstVisit", true); } else { //if this is not the first visit then find the pre-loader - this assumes that the first item with the class name "preload-hold" is the one you want hidden let loader = document.getElementsByClassName("browser-session")[0]; element.classList.remove("browser-session"); } </script> `

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 6, 2020, 10:28am UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/9 "2020-12-06T10:28:52Z")

</div>

Hi @red-rosefields,  
You will just have to make a small change to the `else` section.

```auto
//Find the first element [0] on the page with the targeted class name
let loader = document.getElementsByClassName("your-class-name")[0];
//remove that class name from the element
loader.classList.remove("your-class-name");

```

Make sure to replace “your-class-name” with the actual class name. Also, I’m assuming that the class name you want to remove is the very first on the page. These two lines of code will replace the entire else section from the original script. Everything else will remain the same.

Cheers,  
Bob

---

<div class="post-metadata">

**Author:** ![red-rosefields](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/red-rosefields/32/5392_2.png) [@red-rosefields](https://forum.pinegrow.com/u/red-rosefields)\
**Post date:** [December 8, 2020, 10:22am UTC](https://forum.pinegrow.com/t/have-pinegrow-interaction-run-only-on-first-visit/4713/10 "2020-12-08T10:22:08Z")

</div>

It worked like a charm! Thanks again @RobM
