Developers

CSS Selectors

[data-adsguncode]

Targeting Applied Discount Codes

AdsGun provides a powerful way to style elements based on applied discount codes using CSS attribute selectors. This allows you to dynamically change the appearance of your site when specific codes are active.

[data-adsguncode~="DISCODE"] {
   /* Your styles here */
}

This selector targets elements when the discount code “DISCODE” is applied.

Key Points:

  • The ~= operator matches elements with an attribute value containing a given word, delimited by spaces.
  • To target multiple codes, you need to chain the selectors.
  • The order of the chained selectors doesn’t matter, as it will match regardless of the order the codes were applied.

Examples: Targeting a single code or multiple codes:

[data-adsguncode~="SUMMER2024"] .product-notification,
[data-adsguncode~="BLACKFRIDAY"] .product-notification{
   display: block;
   background-color: #ffff00;
   padding: 10px;
   text-align: center;
}

Adding a site-wide banner for a major sale:

[data-adsguncode~="BIGANNUAL"]::before {
   content: "Annual Big Sale - Extra 20% Off Everything!";
   display: block;
   background-color: #ff0000;
   color: white;
   text-align: center;
   padding: 10px;
   position: fixed;
   top: 0;
   left: 0;
   right: 0;
   z-index: 1000;
}

[data-adsguncode~="BIGANNUAL"] {
   padding-top: 40px;
}

The data-adsguncode attribute is added to the <body> tag. When using pseudo-elements like ::before or ::after, apply them directly to the selector with the data-adsguncode attribute.

JS Events

AdsGun::ready

This event fires when the AdsGun library is loaded and the AdsGun object is attached to the window, but before initialization begins.

window.addEventListener('AdsGun::ready', () => {
   // AdsGun object is available, but not yet initialized
   // Use this for early setup if needed
});

AdsGun::init

This event is dispatched after the initialization completes. The initialization checks for applicable discount codes, applies them, and displays savings.

window.addEventListener('AdsGun::init', () => {
   // AdsGun is fully initialized
   // Discounts have been applied and savings are displayed
   // Safe to use all AdsGun features now
});

JS Functions

AdsGun.activate

The activate function is an asynchronous method that applies discount codes using JavaScript. It accepts a string parameter containing one or more codes, separated by commas if multiple codes are provided.

Parameters

  • codes (string): A single code or multiple codes separated by commas (e.g., "CODE" or "CODE1,CODE2,CODE3")

Returns: A Promise that resolves when the activation process is complete.

Using async/await:

async function applyDiscounts () {
    try {
         await AdsGun.activate("SUMMERSALE");
         console.log("Discount applied successfully");
    } catch (error) {
         console.error("Error applying discount:", error);
    }
}

Using .then():

AdsGun.activate("SUMMERSALE,BLACKFRIDAY")
.then(() => {
    console.log("Discounts applied successfully");
})
.catch((error) => {
    console.error("Error applying discounts:", error);
});

AdsGun.deactivate

The deactivate function is an asynchronous method that deactivates all applied codes. By default, it reloads the page after deactivation, but this behavior can be prevented.

Parameters

  • preventReload (boolean, optional): If set to true, prevents the page from reloading after deactivation. Default is false.

Returns: A Promise that resolves when the deactivation process is complete.

Using async/await:

async function removeDiscounts () {
    try {
         await AdsGun.deactivate();
         // Page will reload by default
    } catch (error) {
         console.error("Error removing discounts:", error);
    }
}

async function removeDiscountsWithoutReload() {
    try {
         await AdsGun.deactivate(true);
         console.log("Discounts removed without page reload");
    } catch (error) {
         console.error("Error removing discounts:", error);
    }
}

Using .then():

AdsGun.deactivate()
.then(() => {
    // Page will reload by default
})
.catch((error) => {
    console.error("Error removing discounts:", error);
});

AdsGun.deactivate(true)
.then(() => {
    console.log("Discounts removed without page reload");
})
.catch((error) => {
    console.error("Error removing discounts:", error);
});