When To Call Methods
There is sometimes confusion around when to call each of the methods in the Granify SDK. This page will focus explicitly on when each method should be called.
activateGranify
Granify.activateGranify(with:)
As the initiating method of the Granify system, this method should be called only once, and when your app starts. Be sure to register all providers and subscribe to all events before calling this method. This method should be called before the app renders the first page.
subscribeGroupAssigned
Granify.subscribeGroupAssigned(handler:)
Call this method only once, immediately before you activate Granify.
subscribeMessageShown
Granify.subscribeMessageShown(handler:)
Call this method only once, immediately before you activate Granify.
registerGroupAssignment
Granify.registerGroupAssignment(provider:)
Call this method only once, immediately before you activate Granify.
setShopperId
Granify.setShopperId(shopperId:)
Call this method only once, as soon after activating Granify as you can. This method must always be called after you have activated Granify. If there is a delay before you get this information (eg: you have to wait for a user to log in, or wait for a server response), call the method as soon as you get the information.
trackPageView
Granify.trackPageView(pageType:parentView:mainScrollView:getInsets:)
Call this method every time a new page view is opened (including the first page). This includes the home page, all collection and product pages, but also every other page in the site.
If your app has been inactive for a time, and is then reactivated, Granify’s session may have timed out, so you will need to call this method again with the same information. It is safe to do this whenever the app regains focus.
See Tracking Pages for more information, and a list of supported page types.
trackProduct
Granify.trackProduct(products:currentProductCarterCallback:)
Every time a new page is a product page, this method must be called. Call this immediately after the trackPageView
method.
If your app has been inactive for a time, and is then reactivated, Granify’s session may have timed out, so you will need to call this method again with the same information. It is safe to do this whenever the app regains focus.
See Tracking Products for more information.
setCurrentProduct
Granify.setCurrentProduct(sku:productId:)
When the shopper is on a product page, this method must be called every time a new variation of the product is selected, including the initial variation.
Call this method immediately after the trackProduct
call, plus whenever a new variant of the product is selected.
If your app has been inactive for a time, and is then reactivated, Granify’s session may have timed out, so you will need to call this method again with the same information. It is safe to do this whenever the app regains focus.
See Tracking Products for more information.
trackCart
Start by calling this method immediately after activateGranify
, when the app starts.
Then, every time the shopper makes a change to the cart in any way, this method must be called. This includes adding, removing, modifying and/or changing the quantities of products in the cart.
If your app has been inactive for a time, and is later reactivated, it is possible that the shopper’s cart has been changed in another app / device in the interim. For this reason, trackCart should be called each time the app regains focus.
See Tracking Products for more information.
Note
You should never call trackCart
and trackCartChange
in the same app. See Which Cart Tracking Method Should I Use? for more details.
trackCartChange
Granify.trackCartChange(add:quantity:)
Granify.trackCartChange(remove:quantity:)
These methods should be called when a change happens to the cart. Whenever:
- A new item is added to the cart
- An existing item in the cart changes its quantity
- An existing item in the cart is removed completely
The method should be called when the change to the cart happens, and not at any other time.
See Tracking Products for more information.
Note
You should never call trackCart
and trackCartChange
in the same app. See Which Cart Tracking Method Should I Use? for more details.
trackOrder
Call this method once an order is finalized and paid. Do not call this when the shopper starts the checkout process - this method is intended only to record completed and finalized orders.
See Tracking Products for more information.
trackWishList
Granify.trackWishList(name:contents:)
Start by calling this method straight after activateGranify
(possibly after the initial trackCart
). Call this method once for each wishlist the user has.
Then, whenever the shopper makes a change to the list, by adding, removing or modifying an item in the list, this method should be called again with the complete updated contents. Again, make a new call for each list.
If you need to wait until the shopper has logged in, and you receive information from the server, you should still call the initial method after activation with an empty list. When the information comes back from the server, call the method again for each list the shopper has.
If the shopper adds a new list, call the method with the new list name and contents when that happens.
If the shopper deletes a list, call the method with the list name and empty contents.
If your app has been inactive for a time, and is then reactivated, it sometimes happens that the shopper’s wishlists have been changed in another app / device in the interim. In this case, it is important to call trackWishList
again with the new information. It is safe to call this method every time the app regains focus, just in case.
setRestrictionState
Granify.setRestrictionState(to:)
After activation, on any page in your app, if, during the operation of the page, a popup or slide-over appears, or for any other reason the page is in a state in which you do not want a campaign to appear, you can call this method to set the Restriction state to .restricted
. This will prevent any campaigns appearing over the page. Note: this does not apply to inline campaigns; they are not hidden while in a restricted state.
Remember to call the method again to set the state to .unrestricted
when you are ready to see campaigns again. The state remains in effect across new page loads, so if you set the state to .restricted
and forget to reset it, no more campaigns will show for that session.
There is also a special state: .pageRestricted
. This will prevent campaigns being seen on the current page, but will be automatically cleared (set to .unrestricted
) whenever you call trackPageView
.
showConcept
Granify.showConcept(conceptId:)
This method is used exclusively for testing. See Testing for details, but essentially you will have added a section in your preferences or settings that allows a developer-user to supply the ID of a concept (campaign). When that happens, you would call this method with that ID.
This method should not be called in any other circumstances. It is not required to make a normal campaigns appear. This is exclusively used to force our Granify Brain to show a campaign that would not normally be allowed to show, in order to test it, and ensure that it displays correctly, and does not adversely affect your app.